简体   繁体   English

Javascript,FileReader与XMLHttpRequest用于读取本地文件?

[英]Javascript, FileReader vs XMLHttpRequest for reading a local file?

I'm trying to read a local file with javascript and a Google Chrome App (it may not be possible through Chrome I think), but I can't see what's the latest approach to the problem. 我正在尝试使用javascript和Google Chrome应用读取本地文件(我认为可能无法通过Chrome读取),但是我看不到解决此问题的最新方法。

I can read it with the following code: 我可以使用以下代码阅读它:

obj.read = function() {
    return new Promise(function(resolve){
        var xmlhttp = new XMLHttpRequest();
        var file_path = 'file_name.xml';
        xmlhttp.open('GET', file_path, true);
        xmlhttp.send(null);
        xmlhttp.onreadystatechange = function() {
            if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                let xml = xmlhttp.responseText;
                var parser = new DOMParser();
                var xmlDoc = parser.parseFromString(xml, "text/xml");
                console.log(xml);
                console.log(xmlDoc);
                resolve(xmlDoc);
            }
        }
    });
};

But it is like I should be using something like 但这就像我应该使用类似

var reader = new FileReader();
reader.onload = (function(theFile) {
    return function(e) {
        console.log(e.target.result);
    };
})(file_path);

var file_path = 'file_name.xml';
var file_parts = [
    new Blob(['you construct a file...'],{type: 'text/plain'}), 
    'Same way as you do with blob',
    new Uint16Array([33])
];
var file = new File(file_parts, file_path);
reader.readAsText(file);

(copied from https://stackoverflow.com/a/24495213/826815 ) (I'm having a hard time finding literature on this topic) (从https://stackoverflow.com/a/24495213/826815复制)(我很难找到有关此主题的文献)

So, what's the way to do it? 那么,该怎么做呢?

The first approach ( XMLHttpRequest for a file in the package) is perfectly valid and likely to be easier if all you need is the whole file. 第一种方法(对包中文件的XMLHttpRequest )是完全有效的,并且如果您只需要整个文件,则可能会更容易。


What you call a "second approach" is lifted from a question about instantiating a File object , not reading an actual file. 您所谓的“第二种方法”摆脱了有关实例化File对象而不是读取实际文件的问题。 This would not allow you to read an existing file, just create a "virtual" file that can be used in DOM forms. 这将不允许您读取现有文件,而只是创建一个可以以DOM形式使用的“虚拟”文件。

There's the whole HTML FileSystem API , which is not implemented anywhere but Chrome and as such documentation is scarce and fraught with big scary red warnings. 有完整的HTML FileSystem API ,除了Chrome之外,其他任何地方都没有实现,因此文档稀少且充满了可怕的红色警告。 You can actually use it for the purpose you state - reading App's own files - by starting with chrome.runtime.getPackageDirectoryEntry , but that's probably overkill in your particular case and again, you'll have a hard time finding examples of doing so. 实际上,您可以通过从chrome.runtime.getPackageDirectoryEntry开始,将其用于陈述的目的-读取App自己的文件,但是在您的特定情况下,这可能会chrome.runtime.getPackageDirectoryEntry ,而且,您将很难找到这样做的例子。

I can think of only two reasons to disturb the slumber of the beast that is FileSystem API for App's own files: 我只能想到打扰野兽的两个原因,即App自己文件的FileSystem API:

  1. You don't know, at runtime, what files are included. 您在运行时不知道包含哪些文件。 Then you can use it to list files . 然后,您可以使用它列出文件 Rare, but possible. 稀有,但可能。
  2. You need a more fine-grained access than "get me the whole thing", for example you only need a chunk of a large file. 您需要比“让我一整件事”更细粒度的访问,例如,您只需要一个大文件的一部分。

Note that FileSystem API is primarily used in Chrome for chrome.fileSystem Apps API. 请注意, FileSystem API主要在Chrome中用于chrome.fileSystem Apps API。 Considering that Chrome is going to support Chrome Apps on Chrome OS for a while, it's likely to stay, despite being non-standard. 考虑到Chrome将在一段时间内支持Chrome OS上的Chrome Apps,尽管它是非标准的,但它可能会保留下来。

Surviving documentation: 尚存文档:

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

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