Is there any way to specify a suggested filename when using data: URI?

前端 未结 16 1650
佛祖请我去吃肉
佛祖请我去吃肉 2020-11-22 03:09

If for example you follow the link:

data:application/octet-stream;base64,SGVsbG8=

The browser will prompt you to download a file consisting of t

16条回答
  •  情歌与酒
    2020-11-22 03:54

    The following Javascript snippet works in Chrome by using the new 'download' attribute of links and simulating a click.

    function downloadWithName(uri, name) {
      var link = document.createElement("a");
      link.download = name;
      link.href = uri;
      link.click();
    }
    

    And the following example shows it's use:

    downloadWithName("data:,Hello%2C%20World!", "helloWorld.txt")
    

提交回复
热议问题