File, FileReader, and Ajax File Upload instance analysis (php) _ javascript skills

Source: Internet
Author: User
File, FileReader, and Ajax File Upload instance analysis (php). For more information, see. What can File FileReader do?
Ajax File Upload example
The FileReader object can read the Base64 encoded data (readAsDataURL), binary string (readAsBinaryString), and text (readAsText) of the file and is asynchronous.
By the way, you can use FileReader and Ajax to upload attachments by dragging the Email.

File object
The File object can be obtained from the input [type = file]. files array and drag event. dataTransfer. files.
The first figure is the File object in Chrome, and the second figure is the File object in Firefox. There are several more methods in Firefox. Note that the Data Reading method here is 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)

The Code is as follows:


Var fileReader = new FileReader ();
FileReader. onloadend = function (){
Console. log (this. readyState); // this time should be 2
Console. log (this. result); reads the completed callback function, and stores the data in the result
}
FileReader. readAsBinaryString (file); // starts to read binary data. asynchronous parameters are file objects.
// FileReader. readAsDataURL (file); // read Base64
// FileReader. readAsText (file); // read text information


You can run the following simple example (chrome and firefox are valid)

The Code is as follows:






Html5 File and FileReader





(Drag the image here) Get the file base64 encoding using FileReader







Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.