简体   繁体   English

通过JavaScript查看上传的数组时遇到问题

[英]Having problems viewing the uploaded array via JavaScript

I am using the File API to upload a file and save its content as a variable in JavaScript. 我正在使用File API上传文件并将其内容另存为JavaScript中的变量。 This is what I have so far but, the result that I get is undefined . 到目前为止,这就是我所得到的,但是我得到的结果是不确定的

 <body>
    <input type="file" id="file" name="file" multiple />

    <script>
      function handleFileSelect(evt) {

        // grab the file that was uploaded which is type File. 
        // evt is the event that was triggered
        // evt.target returns the element that triggered the event 
        // evt.target.files[0] returns the file that was uploaded 
        var file = evt.target.files[0]; 

        // instantiate a FileReader object to read/save the file that was uploaded
        var reader = new FileReader();

        // read the file and save as an array 
        fileArray = reader.readAsArrayBuffer(file);
        window.alert("hello");
        window.alert(fileArray);
      }

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

FileReader is asynchronous. FileReader是异步的。 you should set up onload 你应该设置onload

    var reader = new FileReader();
    reader.onload = function(){
          console.log(reader.result);
        };
       // read the file and save as an array 
   reader.readAsArrayBuffer(file);

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

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