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

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

    Here is a jQuery version based off of Holf's version and works with Chrome and Firefox whereas his version seems to only work with Chrome. It's a little strange to add something to the body to do this but if someone has a better option I'm all for it.

    var exportFileName = "export-" + filename;
    $('<a></a>', {
        "download": exportFileName,
        "href": "data:," + JSON.stringify(exportData, null,5),
        "id": "exportDataID"
    }).appendTo("body")[0].click().remove();
    
    0 讨论(0)
  • 2020-11-22 03:59

    Chrome makes this very simple these days:

    function saveContent(fileContents, fileName)
    {
        var link = document.createElement('a');
        link.download = fileName;
        link.href = 'data:,' + fileContents;
        link.click();
    }
    
    0 讨论(0)
  • 2020-11-22 04:03

    No.

    The entire purpose is that it's a datastream, not a file. The data source should not have any knowledge of the user agent handling it as a file... and it doesn't.

    0 讨论(0)
  • 2020-11-22 04:03

    You actually can achieve this, in Chrome and FireFox.

    Try the following url, it will download the code that was used.

    data:text/html;base64,PGEgaHJlZj0iZGF0YTp0ZXh0L2h0bWw7YmFzZTY0LFBHRWdhSEpsWmowaVVGVlVYMFJCVkVGZlZWSkpYMGhGVWtVaUlHUnZkMjVzYjJGa1BTSjBaWE4wTG1oMGJXd2lQZ284YzJOeWFYQjBQZ3BrYjJOMWJXVnVkQzV4ZFdWeWVWTmxiR1ZqZEc5eUtDZGhKeWt1WTJ4cFkyc29LVHNLUEM5elkzSnBjSFErIiBkb3dubG9hZD0idGVzdC5odG1sIj4KPHNjcmlwdD4KZG9jdW1lbnQucXVlcnlTZWxlY3RvcignYScpLmNsaWNrKCk7Cjwvc2NyaXB0Pg==
    
    0 讨论(0)
提交回复
热议问题