如何在内存中创建文件供用户下载,而不是通过服务器下载? [英] How to create a file in memory for user to download, but not through server?

查看:80
本文介绍了如何在内存中创建文件供用户下载,而不是通过服务器下载?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么方法可以在客户端创建文本文件并提示用户下载文本文件,而无需与服务器进行任何交互?
我知道我不能直接写给他们的机器(安全性和全部),但是我可以创建并提示他们保存吗?

Is there any way I can create a text file on the client side and prompt the user to download it, without any interaction with the server? I know I can't write directly to their machine (security and all), but can I create and prompt them to save it?

推荐答案

您可以使用数据URI。浏览器支持各不相同;请参见维基百科。示例:

You can use data URIs. Browser support varies; see Wikipedia. Example:

<a href="data:application/octet-stream;charset=utf-16le;base64,//5mAG8AbwAgAGIAYQByAAoA">text file</a>

八位字节流将强制进行下载提示。否则,它可能会在浏览器中打开。

The octet-stream is to force a download prompt. Otherwise, it will probably open in the browser.

对于CSV,您可以使用:

For CSV, you can use:

<a href="data:application/octet-stream,field1%2Cfield2%0Afoo%2Cbar%0Agoo%2Cgai%0A">CSV Octet</a>

尝试 jsFiddle演示

这篇关于如何在内存中创建文件供用户下载,而不是通过服务器下载?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆