Code example for asynchronous file upload using Ajax in php

Source: Internet
Author: User

1: Get the file object
2: Read Binary data
3: Simulate http requests and send data (this is usually troublesome)
Use the sendasbinary method of the xmlhttprequest object to send data under forefox;
4: Perfect implementation
Problems encountered
Currently, only firefox can correctly upload files. (Chrome can also be uploaded using google. gears)
The file data read from firefox and chrome is different (I don't know if it is the reason for the debugging tool)
Chrome and other advanced browsers do not have the sendasbinary method and can only use the send method to send data. It may be because the above causes the upload to fail correctly. (Normal text files can be uploaded correctly after testing)
Copy codeThe Code is as follows:
<! Doctype html>
<Html>
<Head>
<Meta http-equiv = "content-type" content = "text/html; charset = UTF-8"/>
<Title> html5 file and filereader </title>
<Link href = "html/ui.css" _ mce_href = "html/ui.css" rel = "stylesheet"/>
</Head>
<Body>
<Style type = "text/css"> <! --
. Box {background: # f8f8f8; border: 1px solid # ccc; padding: 10px;-webkit-box-shadow: #000 0px 0px 4px;-moz-box-shadow: #000 0px 0px 4px;
-Webkit-border-radius: 2px; font-family: 'segoe Ui', calibri, 'myriad Pro', myriad, 'trebuchet Ms', helvetica, arial, sans-serif;
}
. Bl {font-weight: 700 ;}
. Dl {padding: 10px; border-top: 1px dotted #999 ;}
. Dl dd {padding: 0; margin: 0 ;}
. Log {border: 1px solid # ccc; background: # f8f8f8; width: 200px; position: absolute; right: 10px; top: 10px ;}
. Log li {border: 1 p dotted # ccc; word-wrap: break-word; word-break: break-all; margin: 0px; padding: 0 ;}
. Log ul {margin: 0px; padding: 0; list-style: none ;}
--> </Style> <style type = "text/css" _ mce_bogus = "1"> <! --
. Box {background: # f8f8f8; border: 1px solid # ccc; padding: 10px;-webkit-box-shadow: #000 0px 0px 4px;-moz-box-shadow: #000 0px 0px 4px;
-Webkit-border-radius: 2px; font-family: 'segoe Ui', calibri, 'myriad Pro', myriad, 'trebuchet Ms', helvetica, arial, sans-serif;
}
. Bl {font-weight: 700 ;}
. Dl {padding: 10px; border-top: 1px dotted #999 ;}
. Dl dd {padding: 0; margin: 0 ;}
. Log {border: 1px solid # ccc; background: # f8f8f8; width: 200px; position: absolute; right: 10px; top: 10px ;}
. Log li {border: 1 p dotted # ccc; word-wrap: break-word; word-break: break-all; margin: 0px; padding: 0 ;}
. Log ul {margin: 0px; padding: 0; list-style: none ;}
--> </Style>
<Div class = "box" id = "baseinfo">
<H2> (drag the image here) Use filereader to obtain the base64 encoding of the file. <Div> </div>
</Div>
<Div class = "log">
<Ul id = "log">
</Ul>
</Div>
<Script type = "text/CSS"> <! --
(Function (){
Window. datavalue = 0;
Var html = '<dl class = "dl">
<Dd> filename: $ filename $ </dd>
<Dd> filetype: $ filetype $ </dd>
<Dd> filesize: $ filesize $ </dd>
<Dd> </dd>
<Dd> filebase64: <br/>
<Div style = "width: 100%; height: 100px;"> $ filebase64 $ </div>
</Dd>
</Dl>
'
Var log = function (msg ){
// Console ['log'] (msg );
Document. getelementbyid ('log'). innerhtml + = '<li>' + msg + '</li> ';
}
Var dp = function (){
Var defconfig = {
Dropwrap: window
}
This. init. apply (this, [defconfig]);
This. file = null;
}
Dp. prototype = {
Init: function (args ){
Var dropwrap = args. dropwrap;
Var _ this = this;
Dropwrap. addeventlistener ("dragenter", this. _ dragenter, false );
Dropwrap. addeventlistener ("dragover", this. _ dragover, false );
Dropwrap. addeventlistener ('drop', function (e) {_ this. readfile. call (_ this, e)}, false );
Log ('window drop bind -- OK ');
},
_ Dragenter: function (e) {e. stoppropagation (); e. preventdefault ();},
_ Dragover: function (e) {e. stoppropagation (); e. preventdefault ();},
Readfile: function (e ){
E. stoppropagation ();
E. preventdefault ();
Var dt = e. datatransfer;
Var files = dt. files;
For (var I = 0; I <files. length; I ++ ){
Var html = html. slice ();
Html = this. writeheader (files [I], html );
This. read (files [I], html );
}
},
Read: function (file, h ){
Var type = file. type;
Var reader = new filereader ();
Reader. onprogress = function (e ){
If (e. lengthcomputable ){
Log ('Progress: '+ math. ceil (100 * e. loaded/file. size) +' % ')
}
};
Reader. onloadstart = function (e ){
Log ('onloadstart: OK ');
};
Reader. onloadend = function (e ){
Var _ result = e.tar get. result;
// Lelepolic'log'{(e.tar get );
Log ('data uri -- OK ');
Var d = document. createelement ('div ');
H = h. replace ('$ filebase64 $', _ result );
If (/image/. test (file. type )){
H = h. replace ('$ data $', _ result );
}
D. innerhtml = h;
Document. getelementbyid ('baseinfo'). appendchild (d );
};
Reader. readasdataurl (file); // www.3ppt.com base 64 Encoding
Return;
},
Writeheader: function (file, h ){
Log (file. filename + '+ (file. size/1024 ));
Return h. replace ('$ filename $', file. filename ). replace ("$ filesize $", (file. size/1024) + 'kb '). replace ("$ filetype $", file. type );
}
}
New dp ();
})()
// --> </Script>
</Body>
</Html>
Filereader object
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

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.