jQuery: read text file from file system

前端 未结 10 2492
再見小時候
再見小時候 2020-11-30 10:09

I am trying to read a text file using jquery, like this:

// LOAD file and split line by line and append divs
$.get(\'myFile.txt\', function(data) {    
    v         


        
10条回答
  •  一生所求
    2020-11-30 10:39

    If you include a file input box you can access the file as a base64 encoded string by using the FileReader object. If it's a text file a simple base64 decode will work to get the text.

    Assuming the following HTML:

    
    

    You can access using the following JQuery JS:

    var file = $('#myfile').get(0).files[0];
    var reader = new FileReader();
    reader.onload = function (e) {
        //get the file result, split by comma to remove the prefix, then base64 decode the contents
        var decodedText = atob(e.target.result.split(',')[1]);
        //show the file contents
        alert(decoded);
    };
    reader.readAsDataURL(file);
    

提交回复
热议问题