简体   繁体   English

使用Javascript读取客户端文本文件

[英]Reading client side text file using Javascript

I want to read a file (on the client side) and get the content in an array. 我想读取一个文件(在客户端)并获取数组中的内容。 It will be just one file. 它只是一个文件。 I have the following and it doesn't work. 我有以下,它不起作用。 'query_list' is a textarea where I want to display the content of the file. 'query_list'是textarea,我想在其中显示文件的内容。

<input type="file" id="file" name="file" enctype="multipart/form-data"/>

    <script>
       document.getElementById('file').addEventListener('change', readFile, false);

       function readFile (evt) {
           var files = evt.target.files;
           var file = files[0];

          var fh = fopen(file, 0);
          var str = "";
          document.getElementById('query_list').textContent = str;
          if(fh!=-1) {
             length = flength(fh);        
             str = fread(fh, length);     
             fclose(fh);                   
           } 
           document.getElementById('query_list').textContent = str;
        }
      </script>

How should I go about it? 我该怎么办呢? Eventually I want to loop over the array and run some SQL queries. 最终我想循环遍历数组并运行一些SQL查询。

If you want to read files on the client using HTML5's FileReader , you must use Firefox, Chrome or IE 10+. 如果您想使用HTML5的FileReader读取客户端上的文件,则必须使用Firefox,Chrome或IE 10+。 If that is true, the following example reads a text file on the client. 如果是这样,以下示例将读取客户端上的文本文件。

your example attempts to use fopen that I have never heard of (on the client) 您的示例尝试使用我从未听说过的fopen(在客户端上)

http://jsfiddle.net/k3j48zmt/ http://jsfiddle.net/k3j48zmt/

   document.getElementById('file').addEventListener('change', readFile, false);

   function readFile (evt) {
       var files = evt.target.files;
       var file = files[0];           
       var reader = new FileReader();
       reader.onload = function(event) {
         console.log(event.target.result);            
       }
       reader.readAsText(file)
    }

For IE<10 support you need to look into using an ActiveX Object like ADO.Stream Scripting.FileSystemObject http://msdn.microsoft.com/en-us/library/2z9ffy99(v=vs.85).aspx but you'll run into a security problem. 对于IE <10支持,您需要调查使用ActiveX对象,如ADO.Stream Scripting.FileSystemObject http://msdn.microsoft.com/en-us/library/2z9ffy99(v=vs.85).aspx但是你'会遇到安全问题。 If you run IE allowing all ActiveX objects (for your website), it should work. 如果您运行IE允许所有ActiveX对象(对于您的网站),它应该工作。

There is such thing as HTML5 File API to access local files picked by user, without uploading them anywhere. 有一种HTML5 File API可以访问用户选择的本地文件,而无需将它们上传到任何地方。

It is quite new feature, but supported by most of modern browsers . 这是一项非常新的功能,但大多数现代浏览器都支持它。

I strongly recommend to check out this great article to see, how you can use it. 我强烈建议您查看这篇精彩文章 ,了解如何使用它。

There is one problem with this, you can't read big files (~400 MB and larger) because straight forward FileAPI functions attempting to load entire file into memory. 这有一个问题,你无法读取大文件 (~400 MB或更大),因为直接的FileAPI函数试图将整个文件加载到内存中。

If you need to read big files, or search something there, or navigate by line index check my LineNavigator , which allows you to read, navigate and search in files of any size. 如果您需要阅读大文件,或在那里搜索某些内容,或通过行索引导航,请检查我的LineNavigator ,它允许您阅读,导航和搜索任何大小的文件。 Try it in jsFiddle! 在jsFiddle中尝试一下! It is super easy to use: 它非常易于使用:

var navigator = new FileNavigator(file);    

navigator.readSomeLines(0, function linesReadHandler(err, index, lines, eof, progress) {    
    // Some error
    if (err) return;

    // Process this line bucket
    for (var i = 0; i < lines.length; i++) {
        var line = lines[i]; 
        // Do something with it
    }

    // End of file
    if (eof) return;

    // Continue reading
    navigator.readSomeLines(index + lines.length, linesReadHandler);
});

I work there, but still wanted to contribute because it works well: You can use the filepicker.io read api to do exactly this. 我在那里工作,但仍然想贡献,因为它运作良好:你可以使用filepicker.io read api做到这一点。 You can pass in an dom element and get the contents back, for text or binary data, even in IE8+ 您可以传入一个dom元素并获取内容,用于文本或二进制数据,即使在IE8 +中也是如此

Well I got beat to the answer but its different: 好吧,我得到了答案,但它有所不同:

<input type="file" id="fi" />
<button onclick="handleFile(); return false;">click</button>

function handleFile() {
    var preview = document.getElementById('prv')
    var file = document.getElementById('fi').files[0];
    var div = document.body.appendChild(document.createElement("div"));
    div.innerHTML = file.getAsText("utf-8");
}

This will work in FF 3.5 - 3.6, and that's it. 这将适用于FF 3.5 - 3.6,就是这样。 FF 4 and WebKit you need to use the FileReader as mentioned by Juan Mendes. FF 4和WebKit你需要使用Juan Mendes提到的FileReader。

For IE you may find a Flash solution. 对于IE,您可以找到Flash解决方案。

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

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