uniapp使用uni-file-picker實(shí)現(xiàn)上傳功能
html代碼
<uni-file-picker
@select="onFileSelected"
@cancel="onFilePickerCancel"
limit="1"
class="weightPage-upload-but"
file-mediatype="image"
></uni-file-picker>
<image
class="weightPage-item-upload-img"
v-if="weightData.img_url"
:src="weightData.img_url"
mode=""
>
</image>
<image
class="weightPage-item-upload-img"
v-else
src="@/static/checkDetails/upload.png"
mode=""
>
</image>用image覆蓋,就能實(shí)現(xiàn)完全自定義上傳樣式的功能(也能用其他的覆蓋)
上傳前:

上傳后:

功能實(shí)現(xiàn)
data() {
return {
weightData: {
img_url: "",
},
};
},
methods: {
onFileSelected(e) {
// 獲取選中的文件路徑
const filePath = e.tempFiles[0].url;
console.log(filePath);
// 調(diào)用上傳圖片的方法
this.uploadImage(filePath);
},
async uploadImage(filePath) {
try {
let formData = new FormData();
formData.append("file", {
url: filePath,
name: "image.jpg",
type: "image/jpeg",
});
// 轉(zhuǎn)換為普通 Object
const formDataObj = {};
for (let [key, value] of formData.entries()) {
formDataObj[key] = value;
}
/*#ifdef MP-WEIXIN*/
// 從微信小程序的本地緩存中取出 token
let wxToken = wx.getStorageSync("token");
let wxbaseURL = wx.getStorageSync("baseURL");
// 檢查是否成功獲取到值
if (wxToken) {
uni.uploadFile({
url: `${wxbaseURL}...`, //需要傳圖片的后臺接口
filePath: filePath, // 上傳圖片 通過uni-app的uni-file-picker組件 函數(shù)返回
name: "image", //文件名字
header: {
Authorization: "Bearer " + wxToken,
},
formData: formDataObj,
success: (res) => {
const { data } = JSON.parse(res.data);
console.log(data);
this.weightData.img_url = data.url;
uni.showToast({
title: "圖片上傳成功",
icon: "success",
duration: 2000, // 提示持續(xù)時間,單位為毫秒
});
},
fail: (e) => {
console.log(e);
},
});
}
/*#endif*/
/*#ifdef H5*/
let Token = localStorage.getItem("token");
let baseURL = localStorage.getItem("baseURL");
// 檢查是否成功獲取到值
if (Token) {
uni.uploadFile({
url: `${baseURL}...`, //需要傳圖片的后臺接口
filePath: filePath, // 上傳圖片 通過uni-app的uni-file-picker組件 函數(shù)返回
name: "image", //文件名字
header: {
Authorization: "Bearer " + Token,
},
formData: formDataObj,
success: (res) => {
console.log(JSON.parse(res.data));
const { data } = JSON.parse(res.data);
console.log(data);
this.weightData.img_url = data.url;
uni.showToast({
title: "圖片上傳成功",
icon: "success",
duration: 2000, // 提示持續(xù)時間,單位為毫秒
});
},
fail: (e) => {
console.log(e);
},
});
}
/*#endif*/
// 可以添加上傳進(jìn)度監(jiān)聽等額外邏輯
} catch (error) {
console.error("上傳圖片時發(fā)生錯誤", error);
}
},
onFilePickerCancel() {
console.log("取消選擇");
// 可以在這里處理取消選擇的邏輯
},
},css樣式代碼
隱藏樣式的重點(diǎn)是 opacity: 0;
.weightPage-upload-but {
position: absolute;
width: 279px;
height: 100px;
z-index: 1;
left: 0px;
opacity: 0;
padding: 10px;
}
.weightPage-item-upload-img {
width: 80px;
height: 78px;
border-radius: 10px;
}
.weightPage-item-upload-text {
font-size: 12px;
font-weight: 500;
line-height: 19px;
color: rgba(153, 153, 153, 1);
flex: 1;
padding: 10px;
}到此這篇關(guān)于uniapp使用uni-file-picker實(shí)現(xiàn)上傳功能的文章就介紹到這了,更多相關(guān)uniapp uni-file-picker上傳內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
JS跳出循環(huán)的方法區(qū)別對比分析(break,continue,return)
面向?qū)ο缶幊陶Z法中我們會碰到break ,continue, return這三個常用的關(guān)鍵字,那么關(guān)于這三個關(guān)鍵字的使用具體的操作是什么呢?接下來通過本文給大家講解JS跳出循環(huán)的方法區(qū)別對比分析(break,continue,return),感興趣的朋友一起看看吧2023-02-02
純js代碼制作的網(wǎng)頁時鐘特效【附實(shí)例】
下面小編就為大家?guī)硪黄僯s代碼制作的網(wǎng)頁時鐘特效【附實(shí)例】。小編覺得聽錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-03-03
Javascript模仿淘寶信用評價實(shí)例(附源碼)
這篇文章主要介紹了Javascript模仿淘寶信用評價功能實(shí)現(xiàn)方法,以完整實(shí)例形式分析了JavaScript響應(yīng)鼠標(biāo)事件動態(tài)改變頁面元素的相關(guān)技巧,并附帶了完整的實(shí)例代碼供讀者下載參考,需要的朋友可以參考下2015-11-11
javascript實(shí)現(xiàn)左右緩動動畫函數(shù)
這篇文章主要為大家詳細(xì)介紹了javascript實(shí)現(xiàn)左右緩動動畫函數(shù),文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2020-11-11

