读取本地文件,编码到base64,我想给用户一个选项,将结果保存到文件 [英] Reading a local file, encoding to base64, I would like to give the user an option to save the result to file
问题描述
我有一个将本地文件编码到Base64的网页,并希望给用户一个选项来保存结果 - 现在驻留在html元素中,而不返回服务器。
我可以将数据发布到后端网页,保存为文件,并提供< a href = ..但我非常感谢在客户端实现此目的的选项。该文件可以轻松地太大的一个帖子..
我可以写som的JavaScript代码启动保存对话框,并将文件内容推回到用户,如果他选择接受文件?
I have a webpage encoding a local file into Base64, and would like to give the user an option to save the result - now residing in a html element - without going back to the server. I could post the data to a back-end webpage, save as file, and offer a <a href= .. but I would much appreciate the option of fulfilling this on the client side. The file can easily be too big for a post.. Could I write som javascript code to initiate a save dialog, and push the file content back to the user if he chooses to accept the file ?
感谢任何帮助!
善待
Ketil Duna
thanks for any help ! Kind regards Ketil Duna
推荐答案
你可以这样做:
var file = //your Base64 encoded file
var url = 'data:application/octet-stream;base64,' + file;
window.open(url);
您应该调整代码,以便mime类型与您正在投放的内容匹配(应用程序/ pdf .. 。)
You should adapt the code so the mime type matches the content you are serving (application/pdf...)
要传递文件名,您将不得不使用如下所示的锚标签:
To pass a file name you will have to use a anchor tag like follows :
<a href="data:application/octet-stream;base64,/*YOUR FILE DATA*/" download="your filename">
但这不是crossbrowser。
请参阅 http://updates.html5rocks。 com / 2011/08 /下载资源 - HTML5-a-download
but this is not crossbrowser. See http://updates.html5rocks.com/2011/08/Downloading-resources-in-HTML5-a-download
这篇关于读取本地文件,编码到base64,我想给用户一个选项,将结果保存到文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!