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

前端 未结 16 1828
佛祖请我去吃肉
佛祖请我去吃肉 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:52

    This one works with Firefox 43.0 (older not tested):

    dl.js:

    function download() {
      var msg="Hello world!";
      var blob = new File([msg], "hello.bin", {"type": "application/octet-stream"});
    
      var a = document.createElement("a");
      a.href = URL.createObjectURL(blob);
    
      window.location.href=a;
    }
    

    dl.html

    
    
    
        
        Test
        
    
    
    
    
    
    
    

    If button is clicked it offered a file named hello.bin for download. Trick is to use File instead of Blob.

    reference: https://developer.mozilla.org/de/docs/Web/API/File

提交回复
热议问题