export default function asyncUploadFile(option) { return new Promise(function (resolve, reject) { console.log(XMLHttpRequest) if (typeof XMLHttpRequest === 'undefined') { return } let xhr = new XMLHttpRequest() const action = option.action const formData = new FormData() formData.append(option.filename, option.file) console.log('xhr', xhr) xhr.open('post', action, true) xhr.onload = function () { //即使是404也会进入这个相应函数,所以需要检测状态 if (xhr.status < 200 || xhr.status >= 300) { //完成未完成,返回错误 reject(Error(xhr.statusText)) } else { //完成,返回响应文本 resolve(xhr.response) } } // 发生错误时的相应函数 xhr.onerror = function () { reject(Error('Network Error')) } // 发送请求 xhr.send(formData) }) }