Home > Backend Development > PHP Tutorial > Code example of how to implement asynchronous file upload through Ajax in PHP_PHP Tutorial

Code example of how to implement asynchronous file upload through Ajax in PHP_PHP Tutorial

WBOY
Release: 2016-07-21 15:29:22
Original
969 people have browsed it

1: Get the file object
2: Read binary data
3: Simulate http request and send the data (this is usually more troublesome)
Use the sendasbinary method of the xmlhttprequest object under forefox to send data;
4: Perfect implementation
Problems encountered
Currently only firefox can upload files correctly. (Chrome can also use google.gears to upload)
The file data read from Firefox and Chrome seems to be different (I don’t know if it is due to the debugging tool)
Chrome and other advanced browsers do not have sendasbinary method Data can only be sent using the send method. It may be due to the above reasons that the data cannot be uploaded correctly. (After testing, ordinary text files can be uploaded correctly)

Copy code The code is as follows:





html5 file and filereader





(把图片拖拽到这里)利用 filereader 获取文件 base64 编码











filereader对象
var filereader = new filereader();
filereader.onloadend = function(){
console.log(this.readystate); // 这个时候 应该是 2
console.log(this.result); 读取完成回调函数,数据保存在result中
}
filereader.readasbinarystring(file);// 开始读取2进制数据 异步 参数为file 对象
//filereader.readasdataurl(file); // 读取base64
//filereader.readastext(file);//读取文本信息

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/323389.htmlTechArticle1:取得file对象 2:读取2进制数据 3:模拟http请求,把数据发送出去(这里通常比较麻烦) 在forefox下使用 xmlhttprequest 对象的 sendasbinary 方法发送...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template