ajax上传文件是怎么实现的

如题所述

原生js实现方法,如下,可以参考:

var xhr = new XMLHttpRequest();
xhr.addEventListener('progress', function(){
console.log('progress');
}, false);
xhr.addEventListener('load', function(){
console.log('load');
}, false);
xhr.open('post', '/upload', true);///upload为文件上传的接口

var fd = new FormData();
fd.append('upfile', photo.data.file);
fd.append('money', money);
fd.append('source', source);
fd.append('filename', photo.data.name);

xhr.send(fd);
温馨提示:答案为网友推荐,仅供参考