需求:通過接口返回的二進(jìn)制流數(shù)據(jù),這個流數(shù)據(jù)他是一個xlsx文檔,需要給到用戶一個文檔線上連接。
下面是具體代碼,注意只針對二進(jìn)制的文件數(shù)據(jù),如果圖片上傳直接調(diào)用uploadFile就可以,并且兼容原生微信小程序。文章來源:http://www.zghlxwxcb.cn/news/detail-586684.html
export function exportExcel1(query) { uni.showLoading({ title: '正在導(dǎo)出...', mask: true }); // 這個是get接口參數(shù) setUrlQuery 是我自定義的函數(shù)封裝 主要是拼接url參數(shù)的 let obj = { "personnelIdList":state.personnel.list.map((item)=>item.personnelId), "projectId":store.project.projectId, ...query }; // 調(diào)用接口 獲取二進(jìn)制流 進(jìn)行下載并且獲取臨時文件地址 uni.downloadFile({ url:env.ApiUrl+'/personnel/laborer/exportExcel'+uni.setUrlQuery(obj), header: { 'content-type': 'application/json', 'Authorization' : 'Bearer ' + storage.getItem('Authorization') }, success(res){ let tempFilePath = res.tempFilePath; // 獲取微信返回的臨時系統(tǒng)文件地址 uni.uploadFile({ url: env.ApiUrl + 'xxx', // 上傳文件接口 formData: {}, // 除文件外其他所有數(shù)據(jù),傳對象,會默認(rèn)轉(zhuǎn)換為 FormData filePath: tempFilePath, // 上傳臨時的系統(tǒng)文件地址 header: { 'content-type': 'application/json', 'Authorization' : 'Bearer ' + storage.getItem('Authorization') }, name: 'file', // 注意與后端約定的字段名稱 success(res){ uni.hideLoading(); // 彈窗提示導(dǎo)出成功 用戶點擊確定復(fù)制url文件地址 uni.showModal({ title: '導(dǎo)出成功', content: '已生成當(dāng)前列表人員數(shù)據(jù)文檔!', cancelText: '取消', confirmText: '復(fù)制URL', success: function (res) { if (res.confirm) { // 獲取url 進(jìn)行復(fù)制 uni.setClipboardData({ data:result, success() {}, }); } } }); // 文件預(yù)覽 // uni.openDocument({ // filePath: 'url',// 可以是微信給的系統(tǒng)臨時url地址,也可以是正式的線上地址 // success: (sus) => { // console.log('成功打開'); // }, // }); } }); } }); };
?文章來源地址http://www.zghlxwxcb.cn/news/detail-586684.html
到了這里,關(guān)于uniapp小程序(原生微信小程序也可以使用),獲取接口二進(jìn)制流數(shù)據(jù)上傳文件到服務(wù)器的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!