uni-file-picker文件選擇上傳功能實(shí)現(xiàn)
基礎(chǔ)用法
mode="grid" ,可以使用九宮格樣式選擇圖片
limit="1" ,則最多選擇張圖片
file-mediatype="image",限定只選擇圖片
file-extname='png,jpg',限定只選擇 png和jpg后綴的圖片
auto-upload="false",可以停止自動上傳,通過ref調(diào)用upload方法自行選擇上傳時機(jī)。與ref="files"搭配使用,this.$refs.files.upload()。
<uni-file-picker v-model="imageValue" file-mediatype="image" mode="grid" file-extname="png,jpg" :limit="1" @progress="progress" @success="success" @fail="fail" @select="select" /> <script> export default { data() { return { imageValue:[] } }, methods:{ // 獲取上傳狀態(tài) select(e){ console.log('選擇文件:',e) }, // 獲取上傳進(jìn)度 progress(e){ console.log('上傳進(jìn)度:',e) }, // 上傳成功 success(e){ console.log('上傳成功') }, // 上傳失敗 fail(e){ console.log('上傳失?。?,e) } } } </script>
案例一(只上傳一張圖片)
<template> <view class="container example"> <uni-forms ref="baseForm" :modelValue="baseFormData" label-position="top"> <uni-forms-item label="圖片上傳"> <uni-file-picker v-model="imageValue" fileMediatype="image" mode="grid" @select="select" limit="1" ></uni-file-picker> </uni-forms-item> </uni-forms> </view> </template> export default { data() { return { //圖片 imageValue:[], } }, methods:{ //圖片上傳 select(e){ const tempFilePaths = e.tempFilePaths; //獲取圖片臨時路徑 const imgUrl=tempFilePaths[0] uni.uploadFile({ //圖片上傳地址 url: 'https://xxx.xxx.com/api/uploadpic/', filePath: imgUrl, //上傳名字,注意與后臺接收的參數(shù)名一致 name: 'imgUrl', //設(shè)置請求頭 header:{"Content-Type": "multipart/form-data"}, //請求成功,后臺返回自己服務(wù)器上的圖片地址 success: (uploadFileRes) => { console.log('uploadFileRes',JSON.parse(uploadFileRes.data)); //處理數(shù)據(jù) const path=JSON.parse(uploadFileRes.data) //賦值,前端渲染 this.baseFormData.photo=path.imgUrl } }); }, } }
圖片展示
案例二(上傳多張圖片)
<template> <view class="container example"> <uni-forms ref="baseForm" :modelValue="baseFormData" :rules="rules" label-position="top"> <uni-forms-item label="圖片上傳"> <uni-file-picker v-model="imageValue" fileMediatype="image" mode="grid" @select="select" @delete="delIMG" limit="9"></uni-file-picker> </uni-forms-item> </uni-forms> </view> </template> <script> export default { data() { return { imgURL:'', //圖片 imageValue:[], }; }, methods:{ //圖片上傳 select(e){ const tempFilePaths = e.tempFilePaths; const imgUrl=tempFilePaths[0] uni.uploadFile({ url: 'https://xxx.xxx.com/api/uploadpic2/', filePath: imgUrl, name: 'imgUrl', header:{"Content-Type": "multipart/form-data"}, success: (uploadFileRes) => { console.log('uploadFileRes',JSON.parse(uploadFileRes.data)); let path=JSON.parse(uploadFileRes.data) //后端返回的地址,存入圖片地址 this.imageValue.push({ url:this.imgURL+path.imgUrl, name:'' }) console.log('imageValue',this.imageValue) } }); }, //圖片刪除 delIMG(e){ console.log('456',e) const num = this.imageValue.findIndex(v => v.url === e.tempFilePath); this.imageValue.splice(num, 1); } }, onLoad() { //全局定義的圖片訪問地址前綴 this.imgURL=this.$imgURL } } </script>
到此這篇關(guān)于uni-file-picker文件選擇上傳的文章就介紹到這了,更多相關(guān)uni-file-picker文件上傳內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
前端使用URL API實(shí)現(xiàn)高效的URL解析
在現(xiàn)代Web開發(fā)中,URL是前端和后端交互的核心載體,本文將深入探討如何利用URL API實(shí)現(xiàn)URL的解析,構(gòu)建和操作,希望對大家有一定的幫助2025-04-04淺談layui分頁控件field參數(shù)接收對象的問題
今天小編就為大家分享一篇淺談layui分頁控件field參數(shù)接收對象的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-09-09Bootstrap?按鈕下拉菜單的實(shí)現(xiàn)示例
本文主要介紹了Bootstrap?按鈕下拉菜單的實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-07-07Express框架Router?Route?Layer對象使用示例詳解
這篇文章主要為大家介紹了Express框架Router?Route?Layer對象使用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03用JavaScrpt實(shí)現(xiàn)文件夾簡單輕松加密的實(shí)現(xiàn)方法圖文
電腦里經(jīng)常會存儲著重要文件,這些文件需要進(jìn)行加密,有許多方法來實(shí)現(xiàn)。但如果想對一個文件夾里的所有文件都進(jìn)行加密,數(shù)量少還可以,要是數(shù)量多豈不是得把人累死?2008-09-09