如何在锚标记中指定数据- uri资源的文件名
How to specify the filename of a data-URI resource in an anchor tag?
我想使用javascript实现客户端可下载文件,并使用DATA URI在客户端使用以下方式动态创建文件:
<a href="data:application/csv;charset=utf-8,Col1%2CCol2%2CCol3%0AVal1%2CVal2%2CVal3%0AVal11%2CVal22%2CVal33%0AVal111%2CVal222%2CVal333">Download CSV</a>
但是,下载的文件没有名称。我在stackoverflow上看到了一些可以使用'download'属性的解决方案,但我需要支持旧的浏览器,所以我不能使用这个
您可以轻松地根据需要调整这段代码:
//Generate a file name
var fileName = "List_";
//this will remove the blank-spaces from the title and replace it with an underscore
fileName += ReportTitle.replace(/ /g,"_");
//Initialize file format you want csv or xls
var uri = 'data:text/csv;charset=utf-8,' + escape(CSV);
// Now the little tricky part.
// you can use either>> window.open(uri);
// but this will not work in some browsers
// or you will not get the correct file extension
//this trick will generate a temp <a /> tag
var link = document.createElement("a");
link.href = uri;
//set the visibility hidden so it will not effect on your web-layout
link.style = "visibility:hidden";
link.download = fileName + ".csv";
//this part will append the anchor tag and remove it after automatic click
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
a
标签的download
属性现在指定下载文件的名称:
https://html.spec.whatwg.org/multipage/semantics.html attr-hyperlink-download
请看下面的链接。它似乎允许您下载客户端生成的数据并指定文件名。
https://code.google.com/p/download-data-uri/相关文章:
- 如何在Edge中下载图像/png数据URI
- 无声声音数据uri
- Interent Explorer中的数据URI文件下载
- 追加子数据 URI,替换 IE 中的其他对象
- 将数据URI保存在cookie中,以便稍后检索
- 是否可以使用jQuery或PHP上传数据URI
- 使用数据URI提示用户保存文件
- 编辑后将大画布保存为数据URI
- Office.js将数据URI与addFileAttachmentAsync一起使用
- 正在获取跨域内容的数据URI
- 如何从 base64 数据 URI 在服务器端保存 PNG 图像
- 如何嵌入带有数据uri的大图像
- 如何使用dropzone.js收集数据URI内容
s XSS 上的数据 URI 是否可被利用
- 带有 base64 字符串(数据 URI)的 HTML 图像标记
- 如何使用数据 URI 将 MSWord 文档 (.docx) 传送到 Chrome
- 使用数据 URI 将多个文件下载到 Zip 文件中 Javascript
- 如何从 Web API 发布和获取数据 uri 图像
- 如何动态应用 base64 编码的数据 uri
- 到 Blob 的数据 URI 会损坏映像