ade*_*neo 57
是的,您可以使用该download属性在HTML5中执行此类操作
var textToSave = 'this is a test';
var hiddenElement = document.createElement('a');
hiddenElement.href = 'data:attachment/text,' + encodeURI(textToSave);
hiddenElement.target = '_blank';
hiddenElement.download = 'myFile.txt';
hiddenElement.click();
Run Code Online (Sandbox Code Playgroud)
您可以使用 data: URI 就像在 adeneo 的答案中一样,但另一种方法是使用HTML5 Blob 和 createObjectURL(类似地使用下载属性来创建下载链接)。
使用 createObjectURL 的好处是大多数浏览器中的数据 URI 都有严格的大小限制。
来自链接文章的示例代码:
var typedArray = GetTheTypedArraySomehow();
var blob = new Blob([typedArray], {type: 'application/octet-binary'});
// pass a useful mime type here
var url = URL.createObjectURL(blob);
// url will be something like: blob:d3958f5c-0777-0845-9dcf-2cb28783acaf
// now you can use the url in any context that regular URLs can be used
// in, for example img.src, etc.
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
32596 次 |
| 最近记录: |