使用官方api - uni.uploadFile
這是單個文件上傳寫法
/**
* 單個上傳文件方法
*/
fun_GetFileUpload(){
var that = this;
// 請求接口
uni.uploadFile({
url: "url", // 后端接口
formData: {}, // 需要上傳的參數(shù)
filePath: "", // 文件臨時地址
name: 'file', // 后端接收的文件名
header: {}, // 請求頭
success: res => {
console.log(res); // 打印返回內(nèi)容
// 判斷請求是否成功
if (res.statusCode === 200) {
// 判斷是否成功
if(JSON.parse(res.data).result){
// 成功的場合
// 提示
uni.showToast({
title: JSON.parse(res.data).msg,
icon: 'success',
});
}else{
// 提示
uni.showToast({
title: JSON.parse(res.data).msg,
icon: 'error',
});
}
} else {
// 提示
uni.showToast({
title: res.errMsg,
icon: 'error',
});
}
},
fail: err => {
console.log(err)
}
});},
這是上傳多個文件寫法文章來源:http://www.zghlxwxcb.cn/news/detail-852863.html
由于沒有多個上傳文件的方法,目前只能通過遍歷的方式來進(jìn)行多文件上傳文章來源地址http://www.zghlxwxcb.cn/news/detail-852863.html
/**
* 遍歷上傳文件
*/
fun_TraversalFile(){
var that = this;
var frequency = 0; // 這個用于判斷是否遍歷結(jié)束
// 遍歷
that.fileList.map(e=>{
frequency ++; // 遍歷一次+1
// 請求接口
uni.uploadFile({
url: "", // 后端接口
formData: { }, // 需要上傳的參數(shù)
filePath: "", // 文件臨時地址
name: 'file', // 后端接收的文件名
header: {}, // 請求頭
success: res => {
console.log(res); // 打印返回值
// 判斷請求是否成功
if (res.statusCode === 200) {
// 判斷是否成功
if(JSON.parse(res.data).result){
// 成功的場合
// 判斷執(zhí)行結(jié)束
if(frequency === that.fileList.length){ // 判斷遍歷次數(shù)是否與需要上傳的文件數(shù)組長度相同
// 提示
uni.showToast({
title: JSON.parse(res.data).msg,
icon: 'success',
});
}
}else{
// 提示
uni.showToast({
title: JSON.parse(res.data).msg,
icon: 'error',
});
}
} else {
// 提示
uni.showToast({
title: res.errMsg,
icon: 'error',
});
}
},
fail: err => {
console.log(err)
}
});
})
},
到了這里,關(guān)于uniApp、微信小程序上傳單個文件及多個文件的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!