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

前端 未结 16 1825
佛祖请我去吃肉
佛祖请我去吃肉 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;
    $('', {
        "download": exportFileName,
        "href": "data:," + JSON.stringify(exportData, null,5),
        "id": "exportDataID"
    }).appendTo("body")[0].click().remove();
    

提交回复
热议问题