Home>Article>Web Front-end> Knowledge about file reading

Knowledge about file reading

零下一度
零下一度 Original
2017-07-18 16:15:47 1531browse

Recently, a colleague asked me if js can read local files;

I remembered the article I saw about js reading local files before, and then I wrote a demo myself.

ps: This is a bit like Java's IO stream, but there are differences.

Here are some steps I summarized:

1. Get the file object
var file=document.querySelector('input');
2. Set change Event
file.onchange=function(){
   3. Get file resources
   var list=this.files;
   4. Create reader
  var reader=new FileReader() ;
5. Start reading the file
The readAsDataURL() method returns a file path
The readAsText() method returns a file content (string)
reader.readAsDataURL(list[0]);
Get the result after the file reading is completed
are are to be obtained and the result is obtained after the file is read, reader.onload=function(){
##    }
}

In the event, you can look at console.dir(this) in the background; pay attention to the two methods of reading files ①readAsDataURL() method returns a file path ②readAsText() The method returns a file content (string);

The above is the detailed content of Knowledge about file reading. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn