Javascript 使用XHR请求发送文件
嗨,我从一个网站上复制了这段代码,我试图了解它是如何工作的。我想知道将文件发送到php的两种方法之间有什么区别。使用其中一个替代另一个是否有好处?中介绍了btoa功能。似乎并非所有浏览器都实现了此方法 下面是你的例子的例子 第一个示例(sendAsBinary)创建了中定义的表单post数据Javascript 使用XHR请求发送文件,javascript,xmlhttprequest,Javascript,Xmlhttprequest,嗨,我从一个网站上复制了这段代码,我试图了解它是如何工作的。我想知道将文件发送到php的两种方法之间有什么区别。使用其中一个替代另一个是否有好处?中介绍了btoa功能。似乎并非所有浏览器都实现了此方法 下面是你的例子的例子 第一个示例(sendAsBinary)创建了中定义的表单post数据 第二个示例(btoa)使用自定义负载(文件内容的base64编码值)。ah ok这将解释为什么php base64不编码它。谢谢上帝,有那么多东西要学。我能感觉到我的头在膨胀。 if(up.xhr.send
第二个示例(btoa)使用自定义负载(文件内容的base64编码值)。ah ok这将解释为什么php base64不编码它。谢谢上帝,有那么多东西要学。我能感觉到我的头在膨胀。
if(up.xhr.sendAsBinary != null) { //firefox
up.xhr.open('POST', '/html5_upload.php?up=true', true);
var boundary = 'xxxxxxxxx';
var body = '--' + boundary + "\r\n";
body += "Content-Disposition: form-data; name='upload'; filename='" + up.processing.name + "'\r\n";
body += "Content-Type: application/octet-stream\r\n\r\n";
body += binary + "\r\n";
body += '--' + boundary + '--';
up.xhr.setRequestHeader('content-type', 'multipart/form-data; boundary=' + boundary);
up.xhr.sendAsBinary(body);
} else { //for browsers that don't support sendAsBinary yet
up.xhr.open('POST', '/html5_upload.php?up=true&base64=true', true);
up.xhr.setRequestHeader('UP-FILENAME', up.processing.name);
up.xhr.setRequestHeader('UP-SIZE', up.processing.size);
up.xhr.setRequestHeader('UP-TYPE', up.processing.type);
up.xhr.send(window.btoa(binary));
}