简体   繁体   English

读取本地文件,编码为base64,我想给用户一个选项,将结果保存到文件

[英]Reading a local file, encoding to base64, I would like to give the user an option to save the result to file

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. 我有一个将本地文件编码为Base64的网页,并且想给用户一个选项来保存结果-现在位于html元素中-而无需返回服务器。 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. 我可以将数据发布到后端网页上,另存为文件,并提供一个<a href = ..但我非常希望在客户端实现此功能。 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 ? 该文件可能太大而无法张贴。.我可以编写som javascript代码来启动保存对话框,如果用户选择接受文件,则将文件内容推回给用户吗?

thanks for any help ! 谢谢你的帮助 ! Kind regards Ketil Duna 亲切的问候Ketil Duna

You could do it like this: 您可以这样做:

var file = //your Base64 encoded file
var url = 'data:application/octet-stream;base64,' + file;
window.open(url);

You should adapt the code so the mime type matches the content you are serving (application/pdf...) 您应该修改代码,使mime类型与您所服务的内容相匹配(应用程序/ 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">

but this is not crossbrowser. 但这不是跨浏览器。 See http://updates.html5rocks.com/2011/08/Downloading-resources-in-HTML5-a-download 参见http://updates.html5rocks.com/2011/08/Downloading-resources-in-HTML5-a-download

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM