Use html5 FileReader to retrieve images and upload them to the server asynchronously (without iframe ),

Source: Internet
Author: User

Use html5 FileReader to retrieve images and upload them to the server asynchronously (without iframe ),

Use html5 FileReader to retrieve images and upload them to the server asynchronously (without iframe)


Principle:

1. Use FileReader to read the base64 encoding of the image

2. Use ajax to post the base64 encoded image to the server.

3. analyze the image type (jpg, gif, and png) based on the received post data, and generate the corresponding type of image file based on base64_decode.


Html:

<! Doctype html public> 

Server. php

<? Php $ img = isset ($ _ POST ['img '])? $ _ POST ['img ']: ''; // obtain the image list ($ type, $ data) = explode (', ', $ img ); // determine the type if (strstr ($ type, 'image/jpeg ')! = '') {$ Ext = '.jpg ';} elseif (strstr ($ type, 'image/gif ')! = '') {$ Ext = '.gif ';} elseif (strstr ($ type, 'image/png ')! = '') {$ Ext = '.png ';} // generated file name $ photo = time (). $ ext; // generate the file file_put_contents ($ photo, base64_decode ($ data), true); // return header ('content-type: application/json; charset = UTF-8 '); $ ret = array ('img' => $ photo); echo json_encode ($ ret);?>





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.