ajax如何上传文件?
//btn为提交按钮
btn.onclick = function () {
//1.创建对象
var xhr = new XMLHttpRequest();
//2.设置请求参数(post方式)
xhr.open('post', '/upload');
//3 注册回调函数
xhr.onload = function () {
console.log(xhr.responseText);
}
// 上传进度事件
xhr.upload.onprogress = function (event) {
console.log(event.loaded); //以上传的字节
console.log(event.total); //字节总数
}
// 使用formdata对象,发送文件内容
xhr.send(new FormData(document.querySelector('myform')));
}
本文暂时没有评论,来添加一个吧(●'◡'●)