没有服务器端请求 。 所以它必须由纯JS完成。 例如,我有这个代码。
function downloadFile(data, name, mime){ /// what to put here? } var txt="Hello world"; var filename="sample.txt"; var mime="text/plain"; downloadFile(txt,filename, mime);我知道一个库完全在浏览器中完成它而不发送任何http请求。 如果我发送请求,我可以通过服务器端脚本来完成。 只需获取数据并使用某些标题即可触发文件下载。 但我想在Pure JS中做到这一点。 没有服务器端请求。
There will be no server side request. So it has to be done by pure JS. For example I have this code.
function downloadFile(data, name, mime){ /// what to put here? } var txt="Hello world"; var filename="sample.txt"; var mime="text/plain"; downloadFile(txt,filename, mime);I know of a library that does it entirely in browser without sending any http request. If I send a request I can do it by server side script. Just get the data and with some header a file download can be triggered. But I want to do it in Pure JS. NO server-side request.
最满意答案
您链接到的库使用https://github.com/dcneiner/Downloadify/ ,它使用Flash来确保跨浏览器兼容性。 如果您不需要确定它适用于所有浏览器,您可以使用数据uri和纯javascript实现,如下所示: https : //gist.github.com/syntagmatic/2202660 。 您还可以查看新的HTML5文件API: http : //www.html5rocks.com/en/tutorials/file/filesystem/#toc-filesystemurls 。
The library you linked to uses https://github.com/dcneiner/Downloadify/ which uses Flash to ensure cross-browser compatibility. If you don't need to be sure that it works in all browsers, you can use a data uri and a pure javascript implementation as demonstrated here: https://gist.github.com/syntagmatic/2202660. You could also look at the new HTML5 File API: http://www.html5rocks.com/en/tutorials/file/filesystem/#toc-filesystemurls.
更多推荐
发布评论