php ajax xmlhttprequest,使用Ajax XmlHttpRequest上传文件

在尝试使用XMLHttpRequest发送multipart/form-data类型的文件时遇到错误:'请求被拒绝,因为未找到多部分边界'。该问题可能由于缺少正确的Content-Type设置或者多部分数据的分隔符不正确导致。解决方法包括确保正确设置Content-Type头,并正确构造多部分数据的边界。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

嗨,我正在尝试使用此代码发送带有xmlhttprequest的文件。

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);

});

但我收到此错误:请求被拒绝,因为未找到多部分边界,请帮助我。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值