42
嗨,我想用此代碼發送xmlhttprequest文件。使用Ajax上傳文件XmlHttpRequest
<script>
var url= "http://localhost:80/....";
$(document).ready(function(){
document.getElementById('upload').addEventListener('change', function(e) {
var file = this.files[0];
var xhr = new XMLHttpRequest();
xhr.file = file; // not necessary if you create scopes like this
xhr.addEventListener('progress', function(e) {
var done = e.position || e.loaded, total = e.totalSize || e.total;
console.log('xhr progress: ' + (Math.floor(done/total*1000)/10) + '%');
}, false);
if (xhr.upload) {
xhr.upload.onprogress = function(e) {
var done = e.position || e.loaded, total = e.totalSize || e.total;
console.log('xhr.upload progress: ' + done + '/' + total + ' = ' + (Math.floor(done/total*1000)/10) + '%');
};
}
xhr.onreadystatechange = function(e) {
if (4 == this.readyState) {
console.log(['xhr upload complete', e]);
}
};
xhr.open('post', url, true);
xhr.setRequestHeader("Content-Type","multipart/form-data");
xhr.send(file);
}, false);
});
</script>
,但我得到這個錯誤:請求被拒絕,因爲沒有多邊界發現 幫我請..
ty這麼多幫助我.. – 2011-06-02 11:48:07
xhr.send(文件)基於XHR2,它是XMLHttpRequest對象的新版本,僅在某些瀏覽器中可用。 – nkassis 2012-04-20 19:19:16
@nkassis是的,但它會發送文件本身作爲帖子正文,而不是構建服務器解析的多部分postdata。 – timdream 2012-04-22 18:19:48