上傳文件(支持一次上傳多個(gè)文件)
let axiosPostRequestCancel = null
function uploadFiles(data, progressCallBack, callBack) {
let formData = new FormData();
data.map((item,index)=>{
let file = {
uri: 本地文件絕對(duì)路徑,
type: 'application/octet-stream',
name: 文件名字
};
formData.append("file", file);
})
let config = {
//添加請(qǐng)求頭
headers: { "Content-Type": "multipart/form-data" },
timeout: 600000,
//添加上傳進(jìn)度監(jiān)聽事件
onUploadProgress: e => {
let completeProgress = (e.loaded / e.total * 100) | 0;
progressCallBack && progressCallBack(completeProgress)
},
cancelToken: new axios.CancelToken(function executor(c) {
axiosPostRequestCancel = c // 用于取消上傳
})
};
axios.post(接口地址, formData, config)
.then(
function (response)
{
callBack && callBack(true, response)
})
.catch(function (error) {
callBack && callBack(false)
});
}
/**
* [cancelAxiosRequest 取消axios post請(qǐng)求]
*/
function cancelAxiosRequest(){
axiosPostRequestCancel && axiosPostRequestCancel('cancel')
axiosPostRequestCancel = null
}
下載圖片
global.Buffer = global.Buffer || require('buffer').Buffer
axios.get(imageUri,{responseType:'arraybuffer'})
.then((response)=> Buffer.from(response.data, 'binary').toString('base64'))
.then((res)=>{
console.log('data:image/jpeg;base64,'+res);
}).catch(e=>{
console.log('eeee=',e);
})
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者