简体   繁体   English

如何使用javascript中的浏览按钮读取所选文件的内容

[英]How to read the contents of the file selected using browse button in javascript

I have browsed for files using 我已经使用浏览了文件

      <input id="files" type="file" multiple/>
              <output id="result1"></output>

Inorder to read the contents of the browsed file I have used 为了读取我使用过的浏览文件的内容

    window.onload = function(){

    if(window.File && window.FileList && window.FileReader)
    {
        var filesInput = document.getElementById("files");
        //console.log(filesInput);
        filesInput.addEventListener("change", function(event){

            var files = event.target.files; //FileList object
            var output1 = document.getElementById("result1");

            for(var i = 0; i< files.length; i++)
            {
                var file = files[i];

                 var picReader = new FileReader();

                picReader.addEventListener("load",function(event){

                    var picFile = event.target;

                    var div1 = document.createElement("div1");

                  /*  div1.innerHTML = "<img class='thumbnail' src='" + picFile.result + "'" +
                            "title='" + picFile.name + "'/>";*/


         valid1(picFile.result);

                });

                 //Read the data
                picReader.readAsDataURL(file);
            }                               

        });
    }
    else
    {
        console.log("Your browser does not support File API");
    }
}

Here i am passing the contents read to a function valid1(). 在这里,我将读取的内容传递给函数valid1()。 The issue with this code is that as soon as i browse the file, automatically its contents are read due to the window.onload function. 这段代码的问题在于,一旦我浏览该文件,由于window.onload函数的存在,它的内容将自动被读取。

Can someone please suggest so as to how i can modify the same function so that the onload part is removed and instead is replaced by onclick function..so that on the click of a button the contents should be read using filereader.readasdataurl() instead of automatically being read using window.onload()..I can't do it directly because the eventlisteners have to be changed accordingly...can anyone help me out with this.. 有人可以建议我如何修改相同的功能,以便删除onload部分,而由onclick函数代替。以便单击按钮时,应使用filereader.readasdataurl()读取内容。使用window.onload()自动读取。我无法直接执行此操作,因为必须相应地更改事件侦听器...任何人都可以帮我解决这个问题。

var btn = document.getElementById('my-button-id');

btn.onclick = function(){
    // same as before
}

The file is not read because of the onload function, it's read because of the "change" event listener. 由于onload函数,无法读取文件,而由于“ change”事件侦听器,因此已读取文件。

filesInput.addEventListener("change", function(event){

Change it to: 更改为:

** edite via KWieiss's answer ** **通过KWieiss的答案进行编辑**

var btn = document.getElementById('files');

btn.onclick = function() {
// your code here.
}

This places the file read handler on the button click event. 这会将文件读取处理程序放在按钮click事件上。

Change the event to a button click, and change references to files. 将事件更改为单击按钮,然后更改对文件的引用。

Note the accept="image/*" , it could help to the user in selecting only images (however, the browser is not checking the file type, only help selecting a file) 请注意accept="image/*" ,它可以帮助用户仅选择图像(但是,浏览器不检查文件类型,仅帮助选择文件)

See this fiddle https://jsfiddle.net/FranIg/azrbsgpe/ 看到这个小提琴https://jsfiddle.net/FranIg/azrbsgpe/

In your HTML: 在您的HTML中:

<input id="files" type="file" multiple accept="image/*" />
<button type="button" id="btn">
Load</button>
</button>
<output id="result1"></output>

In your script: 在脚本中:

window.onload = function(){

    if(window.File && window.FileList && window.FileReader)
    {

        var btnClick = document.getElementById("btn");
        //console.log(filesInput);
        btnClick.addEventListener("click", function(event){

            var files = document.getElementById("files").files; //FileList object
            var output1 = document.getElementById("result1");

            for(var i = 0; i< files.length; i++)
            {
                var file = files[i];

                 var picReader = new FileReader();

                picReader.addEventListener("load",function(event){

                    var picFile = event.target;

                    var div1 = document.createElement("div1");

                  /*  div1.innerHTML = "<img class='thumbnail' src='" + picFile.result + "'" +
                            "title='" + picFile.name + "'/>";*/


         valid1(picFile.result);

                });

                 //Read the data
                picReader.readAsDataURL(file);
            }                               

        });
    }
    else
    {
        console.log("Your browser does not support File API");
    }
}

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

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