通过纯javascript下载触发器文件(Trigger file download by pure javascript)

编程入门 行业动态 更新时间:2024-10-22 08:00:44
通过纯javascript下载触发器文件(Trigger file download by pure javascript)

没有服务器端请求 。 所以它必须由纯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.

更多推荐

本文发布于:2023-07-24 10:51:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1244993.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:触发器   文件   javascript   Trigger   pure

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!