Bookmarklet to save file

不问归期 提交于 2019-12-07 12:12:53

问题


Is is possible to use a bookmarklet in Firefox to save/open a file directly?

Many bookmarklets open a page on which one can click on a link to download the result. E.g. using a blob link. Is it possible to avoid this extra click and invoke the "save file" / "open file" dialog directly?


回答1:


To trigger the "Save As" dialog for any resource (blob:, http:, whatever permitted scheme), use the download attribute of an anchor. This is supported since Firefox 20.

Example: A bookmarklet that presents the current page as a download:

javascript:(function() {
    var a = document.createElement('a');
    a.href = location.href;
    a.download = 'filename.html';
    document.body.appendChild(a);
    a.click();
    a.parentNode.removeChild(a);
})();

To trigger the Open dialog, create an <input type="file">, and click() it. For many examples, see Using files from web applications.




回答2:


Should be possible if the bookmarklet sends you to a page where the web server sends the appropriate headers to force a download. Example:

Content-Disposition: attachment; filename="filename.zip"
Content-Type: application/zip


来源:https://stackoverflow.com/questions/17900671/bookmarklet-to-save-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!