Chrome extension : Download/export content created “on the fly”

主宰稳场 提交于 2019-12-30 04:42:06

问题


I need to let the user to download a file created on the fly from some data contained in the extension, but I don't want to do this server-side.

As a real-world example : There is a variable containing the text "hello world". I want the user to be able to download/create a .TXT file containing such text.

Is it possible ?

-edited Oct 30, 2010-

Solutions so far:

window.saveAs - not working at all

createObjectURL - works, but the filename and other options can't be customized yet

copy data to clipboard - works, but too many steps are required to the user

create a GoogleDocs document - could work, but needs messing with the API and Oauth

-edited Apr 15, 2011

Looks like things are improving: https://github.com/eligrey/FileSaver.js

But Chrome 14+ is still a bit weird about filenames.


回答1:


I think the only way is to call save dialog through flash, see Downloadify library.




回答2:


In the Chromium-extensions Google Group I have found this working example: (I have modified it to work from the pop-up)

BuiltBlob = new BlobBuilder(""); 
BuiltBlob.append("Hello, world"); 
BlobToSave = BuiltBlob.getBlob(); 
chrome.tabs.create({'url': createObjectURL(BlobToSave), 'selected': false});

But the filename is not set, ending with something like cf8a56bf-d724-4b97-b10f-e252961135bd

On the The W3C docs ( http://dev.w3.org/2009/dap/file-system/file-writer.html ) I've found this not working example:

var bb = new BlobBuilder(); 
bb.append("Lorem ipsum"); 
var fileSaver = window.saveAs(bb.getBlob(), "test_file"); 
fileSaver.onwriteend = myOnWriteEnd; 

but window.saveAs doesn't appear to exists.

Googleing around I've found outdated Google Gears references, but nothing else, maybe because I'm dealing with something too new to have proper documentation ?

Is there a way to set the filename/mime-type to the first example?




回答3:


I don't think this is possible because of safety reasons



来源:https://stackoverflow.com/questions/4003352/chrome-extension-download-export-content-created-on-the-fly

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