File, FileReader and Ajax file upload example analysis (php)_javascript skills
WBOY
Release: 2016-05-16 18:07:13
Original
2035 people have browsed it
File What can FileReader do? Ajax file upload example FileReader object can read the Base64 encoded data of the file (readAsDataURL), binary string (readAsBinaryString), text (readAsText) and all asynchronously. By the way, you can use FileReader and Ajax to upload email attachments by dragging and dropping them.
File object File object can be obtained from the input[type=file].files array, and the drag event event.dataTransfer.files. The first picture is the File object under Chrome, and the second picture is the File object under Firefox. There will be several more methods under Firefox. Note that the data reading methods here are synchronous.
FileReader object This is used to read file data (and is asynchronous). The following is a simple code (the file object is obtained using the above method)
var fileReader = new FileReader(); fileReader.onloadend = function(){ console.log(this.readyState); // This time should be 2 console.log(this. result); Reading completion callback function, the data is saved in result } fileReader.readAsBinaryString(file);//Start reading binary data asynchronously. The asynchronous parameter is the file object //fileReader.readAsDataURL (file); //Read Base64 //fileReader.readAsText(file);//Read text information
You can run the following simple example (valid for chrome and firefox)
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