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文件選擇上傳功能實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)uni-file-picker文件上傳內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
使用JSX 建立 Markup 組件風(fēng)格開發(fā)的示例(前端組件化)
這篇文章主要介紹了使用JSX 建立 Markup 組件風(fēng)格開發(fā)的示例(前端組件化)本文重點(diǎn)講解如何從0搭建一個組件系統(tǒng),在這里我們會學(xué)習(xí)使用JSX來建立markup 的風(fēng)格,我們基于與React 一樣的 JSX 去建立我們組件的風(fēng)格2021-04-04window.print打印指定div指定網(wǎng)頁指定區(qū)域的方法
這篇文章主要介紹了window.print打印指定div指定網(wǎng)頁指定區(qū)域的方法,需要的朋友可以參考下2014-08-08mvvm雙向綁定機(jī)制的原理和實(shí)現(xiàn)代碼(推薦)
下面小編就為大家?guī)硪黄猰vvm雙向綁定機(jī)制的原理和實(shí)現(xiàn)代碼(推薦)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-06-06JS實(shí)現(xiàn)重新加載當(dāng)前頁面或者父頁面的幾種方法
本文介紹了JS實(shí)現(xiàn)重新加載當(dāng)前頁面或者父頁面的幾種方法.需要的朋友可以參考下2016-11-11js重寫alert控件(適合學(xué)習(xí)js的新手朋友)
這篇文章主要介紹js重寫alert控件的過程比較適合學(xué)習(xí)js的新手朋友,需要的朋友可以參考下2014-08-08Bootstrap導(dǎo)航簡單實(shí)現(xiàn)代碼
這篇文章主要介紹了Bootstrap導(dǎo)航的簡單實(shí)現(xiàn)代碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-03-03js is_valid_filename驗(yàn)證文件名的函數(shù)
有時候我們需要對文件名進(jìn)行控制,包括一些特殊命名的文件與特殊符號的文件名進(jìn)程替換,那么就可以使用下面的函數(shù)2017-07-07