需求:
编写一个文件上传函数,使得能够返回文件的上传进度
经过查询发现axios中有onUpdateProgress监听文件上传。
实践如下:
src/utils/upload.js
import axios from 'axios'
import { getToken } from '@/utils/auth'
export function upload_test(url, data, callback) {
return new Promise((resolve, reject) => {
axios.post(url, data, {
headers: {
'Content-Type': 'multipart/form-data',
'Authorization': 'Bearer ' + getToken(),
},
onUploadProgress: progressEvent => {
callback && callback(progressEvent)
},
}).then(response => {
resolve(response)
}).catch(error => {
reject(error)
})
})
}
src/api/upload.js
import request from '@/utils/request'
import { upload_test } from '@/utils/upload'
// 上传文件_进度
export function uploading_test(data,callback) {
return
|