Html 使用 HTML5 FileSystem API 将文件写入桌面

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19744155/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 14:59:08  来源:igfitidea点击:

Writing file to desktop using HTML5 FileSystem API

javascripthtmlfileapi

提问by Johan

I'm playing around a bit with the FileSystem API.

我正在玩弄FileSystem API

I've found a lot of examples where you generate a download link and let the user download the file the "browser way".

我发现了很多示例,您可以在其中生成下载链接并让用户以“浏览器方式”下载文件。

I would like to know two things:

我想知道两件事:

  1. Is there any way to write the ajax result in the fiddle as a file directly to the disk (without any type of prompt). Like to the user's desktop for example.

  2. Is blob the most suitable format for this?

  1. 有没有办法将小提琴中的ajax结果作为文件直接写入磁盘(没有任何类型的提示)。以用户的桌面为例。

  2. blob 是最合适的格式吗?

http://jsfiddle.net/FBGDe/

http://jsfiddle.net/FBGDe/

var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function(){
    if (this.readyState == 4 && this.status == 200){
        console.log(this.response, typeof this.response);
        var img = document.getElementById('img');
        var url = window.URL = window.webkitURL;
        img.src = url.createObjectURL(this.response);
    }
}
xhr.open('GET', 'http://www.newyorker.com/online/blogs/photobooth
                                                       /NASAEarth-01.jpg');
xhr.responseType = 'blob';
xhr.send();      

采纳答案by Paul Draper

Unfortunately, writing to regular files is not currently possible (despite the accepted answer Modifying Local Files Using HTML5 and JavaScript).

不幸的是,目前无法写入常规文件(尽管接受了使用 HTML5 和 JavaScript 修改本地文件的答案)。

You can only write to the sandboxed filesystem.

您只能写入沙盒文件系统。

FYI, you can do this in a Chrome Packaged App: http://developer.chrome.com/apps/fileSystem.htmlBut even then the user must at least choose the file first. Writing to any file would be a serious security hole.

仅供参考,您可以在 Chrome Packaged App 中执行此操作:http: //developer.chrome.com/apps/fileSystem.html但即便如此,用户至少必须先选择文件。写入任何文件将是一个严重的安全漏洞。

What problem are you really trying to solve?

你真正想解决什么问题?

回答by Aparajith Sairam

Please note that Filesystem API is no longer part of the standard's specification, as specified at: http://www.w3.org/TR/file-system-api/

请注意,文件系统 API 不再是标准规范的一部分,具体如下:http: //www.w3.org/TR/file-system-api/

EDIT: Quoting the specification in case the link changes: "File API: Directories and System W3C Working Group Note 24 April 2014

编辑:引用规范以防链接更改:“文件 API:目录和系统 W3C 工作组注释 2014 年 4 月 24 日

Work on this document has been discontinued and it should not be referenced or used as a basis for implementation."

本文件的工作已停止,不应作为参考或用作实施的基础。

(This does not relate to the question directly, but it is essential to know not to use the FileSystem API further.)

(这与问题没有直接关系,但必须知道不要进一步使用 FileSystem API。)

Another link: http://www.html5rocks.com/en/tutorials/file/filesystem/

另一个链接:http: //www.html5rocks.com/en/tutorials/file/filesystem/

"In April 2014, it was announced on public-webapps that the Filesystem API spec should be considered dead. Other browsers have showed little interest in implementing it."

“2014 年 4 月,公共网络应用程序宣布文件系统 API 规范应该被视为已死。其他浏览器对实现它几乎没有兴趣。”