Using FileReader to encode files as Base64 and upload _javascript skills via Ajax

Source: Internet
Author: User
Tags base64

Using Ajax is not directly uploaded files, is generally a new iframe in it to complete the form submission process to achieve the effect of asynchronous upload files.
Doing so can achieve better browser compatibility, but the amount of code will be relatively large, even with the use of file upload plug-ins, such as Plupload.

How can you be flexible enough to treat a file as a normal form parameter like a normal AJAX submission form data?

Flash, the use of JavaScript FileReader object to encode the file into a base64 and then send the server is not OK ~

Begin to have plenty of clothing.

The front end encodes the file and transmits it to the server via Ajax: Base64

 
 

The back end decodes and saves the file data:

<?php

if (isset ($_post[' file_base64 ')) {
  $file _base64 = $_post[' file_base64 '];
  $file _base64 = preg_replace ('/data:.*;base64,/i ', ', $file _base64);
  $file _base64 = Base64_decode ($file _base64);

  File_put_contents ('./file.save ', $file _base64);
}

JavaScript in the FileReader object mainstream browser support, IE10 above support, private think in order to provide services for small scope can consider this asynchronous upload file way, save time and effort, compatible IE series is another matter.

Related Article

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.