Using FileReader to encode a file into Base64 and then using AJAX to upload _ javascript

Source: Internet
Author: User
This article describes how to use FileReader to encode a file into Base64 and upload it via AJAX, including the PHP code that the backend decodes and stores the file data, for more information about how to use AJAX, you cannot directly upload files. Generally, you can create a new iframe to complete the form submission process in it to achieve asynchronous file upload.
This can achieve better browser compatibility, but the amount of code will be relatively large, even if the file upload plug-in is used, such as plupload.

How can we achieve flexibility? just treat a file as a common form parameter like submitting form data through AJAX.

With a flash of light, it's okay to use the javascript FileReader object to encode the file into base64 and then transfer it to the server ~

Start to work.

The front end uses base64 to encode the file and transmits it to the server through ajax:

  
 

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.