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.