简体   繁体   中英

selecting a file using location url instead of input file in html5 filereader for javascript

i want to give the file location url for the code to get my file instead of using input file in html part , to pass the file to the code

the code pasted below works if i use " input type= "file" " to get the file, but if i use url (like below) it gives a error

fileInput1.addEventListener is not a function
at window.onload 

here is the code

window.onload = function() {
var z ="C:/Users/akash/Desktop/riidl/UTham.txt"
    var fileInput1 = z;
if (fileInput1){
    fileInput1.addEventListener('change', function(e) {
        var file = fileInput1.files[0];
        var textType = /text.*/;

        if (file.type.match(textType)) {
            var reader = new FileReader();
            reader.onload = function(e) {
                spamdata=reader.result;
                document.getElementById('here').onclick = console.log(spamdata);

              }    
            reader.readAsText(file);                    
        } 
    });
}

}

Accessing local files is not allowed in JavaScript for security purposes.

Pl refer to this answer for more details.

https://stackoverflow.com/a/372333/3626796

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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