uniapp在開發(fā)app時上傳文件時的問題記錄
更新時間:2024年09月13日 09:20:43 作者:BUG創(chuàng)建者
在開發(fā)uniapp應用時,可能會遇到文件上傳功能在iOS和部分Android手機上不兼容的問題,經過對比分析,發(fā)現問題可能出在文件的路徑上,通過使用uni.saveFile方法保存文件后,再上傳可以解決問題,這篇文章詳細介紹了解決方案,并引導讀者參考更多相關內容
手機拍照然后上傳沒問題 但是在相冊中選擇的照片上傳 ios手機不行 安卓一部分手機也點擊沒反應
最后對比了下參數 發(fā)現路徑有所不同
使用uni.saveFile保存路徑好在重新上傳
saveFileSync(tempFilePath){ return new Promise((resolve, reject) => { uni.saveFile({ tempFilePath, success: function (file) { resolve(file.savedFilePath) }, fail: function (error) { reject(error) } }) }) },
uni.chooseImage({ count: 1, //默認9 sizeType: ["compressed"], //可以指定是原圖還是壓縮圖,默認二者都 sourceType: ['camera','album'], success: async function(result) { let ewm = result.tempFiles[0] const path = await that.saveFileSync(ewm.path) if (result.errMsg === "chooseImage:ok") { result.tempFiles[0].path=path // that.upload(path); that.upload(result.tempFiles[0]); } else { uni.showToast({ title: "圖片上傳失敗", icon: "none", }); } }, fail(err) { uni.showToast({ title: "取消上傳", icon: "none", }); }, });
Upload(event) { const token = this.getToken(); // const url = this.getuploadUrl(); const imgList = []; uni.showLoading({ title: "上傳中...", mask: true, }); try { const [err, res] = await uni.uploadFile({ url: `${HOST}/resource/file/upload`, filePath: event.path, name: "file", header: { Authorization: token, }, }); if (res && (res.statusCode === 200)) { const result = JSON.parse(res.data); if (result.code == 200) { let res1 = JSON.parse(res.data); res1.data.uuid = res1.data.id; res1.data.paramskey = event.name; imgList.push(res1.data); const list = [...this.list, ...imgList]; this.$emit("value", list); this.$emit("change", list); this.$emit("upload", imgList); } else { wx.showToast({ icon: "none", title: result.msg, }); } } else { wx.showToast({ icon: "error", title: "上傳失敗", }); } } catch (error) { console.log(error) } uni.hideLoading(); this.$emit("upload", imgList); },
到此這篇關于uniapp在開發(fā)app時上傳文件時的問題的文章就介紹到這了,更多相關uniapp上傳文件內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
基于MooTools的很有創(chuàng)意的滾動條時鐘動畫
一款很有創(chuàng)意的時鐘js動畫.是利用系統滾動條來構成一個 時:分:秒 的盤. 再利用滾動條的長度變化做過渡動畫.2010-11-11JavaScript函數式編程(Functional Programming)純函數用法分析
這篇文章主要介紹了JavaScript函數式編程(Functional Programming)純函數用法,結合實例形式分析了javascript函數式編程中純函數的函數依賴、所指透明等概念相關原理及使用技巧,需要的朋友可以參考下2019-05-05