uniapp上傳二進(jìn)制圖片的實(shí)現(xiàn)
功能需求:
前端選擇本地文件,將選擇好的文件顯示在界面上進(jìn)行預(yù)覽,可同時(shí)選擇四張進(jìn)行預(yù)覽。
思路如下:
前端選擇本地的png、jpg、等格式的圖片,將圖片以二進(jìn)制的形式傳到后端服務(wù)器,后端對(duì)二進(jìn)制圖片進(jìn)行處理,返回給前端一個(gè)服務(wù)器鏈接在線圖片,在瀏覽器就可以打開鏈接訪問的那種。然后前端將這個(gè)圖片鏈接渲染在頁面進(jìn)行預(yù)覽。
首先
我們看一下uniapp的官方文檔:https://uniapp.dcloud.io/api/media/image?id=chooseimage
大概是這樣的
先寫一個(gè)模擬的demo
1:首先我是是用了colorUI的框架,在項(xiàng)目里面引入
在page底下的vue文件引入
@import "../../colorui/main.css"; @import "../../colorui/icon.css";
這樣一來,就不需要寫什么樣式了,直接使用寫好的就行了。
<template>
<view>
<form>
<view class="cu-bar bg-white margin-top">
<view class="action">
圖片上傳
</view>
<view class="action">
{{imgList.length}}/4
</view>
</view>
<view class="cu-form-group">
<view class="grid col-4 grid-square flex-sub">
<view class="bg-img" v-for="(item,index) in imgList" :key="index" @tap="ViewImage" :data-url="imgList[index]">
<image :src="imgList[index]" mode="aspectFill"></image>
<view class="cu-tag bg-red" @tap.stop="DelImg" :data-index="index">
<text class='cuIcon-close'></text>
</view>
</view>
<view class="solids" @tap="ChooseImage" v-if="imgList.length<4">
<text class='cuIcon-cameraadd'></text>
</view>
</view>
</view>
</form>
</view>
</template>
<script>
export default {
data() {
return {
imgList: [],
// modalName: null,
};
},
methods: {
ChooseImage() {
uni.chooseImage({
count: 4, //默認(rèn)9
sizeType: ['original', 'compressed'], //可以指定是原圖還是壓縮圖,默認(rèn)二者都有
sourceType: ['album'], //從相冊(cè)選擇
success: (res) => {
if (this.imgList.length != 0) {
this.imgList = this.imgList.concat(res.tempFilePaths)
} else {
this.imgList = res.tempFilePaths
}
}
});
},
ViewImage(e) {
uni.previewImage({
urls: this.imgList,
current: e.currentTarget.dataset.url
});
},
//刪除
DelImg(e) {
uni.showModal({
title: '刪除',
content: '確定要?jiǎng)h除這張圖片?',
cancelText: '取消',
confirmText: '刪除',
success: res => {
if (res.confirm) {
this.imgList.splice(e.currentTarget.dataset.index, 1)
}
}
})
},
}
}
</script>
<style>
@import "../../colorui/main.css";
@import "../../colorui/icon.css";
.cu-form-group .title {
min-width: calc(4em + 15px);
}
</style>效果是這樣的
每次選完圖片之后顯示在頁面上,我這里設(shè)置了最多可以選擇四張,圖片鏈接使用了臨時(shí)的blob,接下來就要使用后端小伙伴給的接口,將自己本地的二進(jìn)制文件傳給他了。
在chooseImage選擇好圖片之后,寫一個(gè)成功的回調(diào)函數(shù),在回到函數(shù)里面添加一個(gè)圖片上傳的方法uploadFile,在方法里面添加url,等參數(shù)。

success: (res) => {
const tempFilePaths = res.tempFilePaths;
const uploadTask = uni.uploadFile({
url: 'http://47.xxx.xxx.78:8088/chemApp/work/upload.action',
filePath: tempFilePaths[0],
name: 'file',
success: function(uploadFileRes) {
console.log(uploadFileRes);
_this.imgList = [..._this.imgList, uploadFileRes.data]
}
});
}若是請(qǐng)求成功
則返回一個(gè)圖片鏈接
添加接口之后 的,demo如下:
<template>
<view>
<form>
<view class="cu-bar bg-white margin-top">
<view class="action">
圖片上傳
</view>
<view class="action">
{{imgList.length}}/4
</view>
</view>
<view class="cu-form-group">
<view class="grid col-4 grid-square flex-sub">
<view class="bg-img" v-for="(item,index) in imgList" :key="index" @tap="ViewImage" :data-url="item">
<image v-if="item" :src="item" mode="aspectFill"></image>
<view class="cu-tag bg-red" @tap.stop="DelImg" :data-index="index">
<text class='cuIcon-close'></text>
</view>
</view>
<view class="solids" @tap="ChooseImage" v-if="imgList.length<4">
<text class='cuIcon-cameraadd'></text>
</view>
</view>
</view>
</form>
</view>
</template>
<script>
export default {
data() {
return {
imgList: [],
// modalName: null,
};
},
methods: {
ChooseImage() {
const _this = this
uni.chooseImage({
count: 4, //默認(rèn)9
sizeType: ['original', 'compressed'], //可以指定是原圖還是壓縮圖,默認(rèn)二者都有
sourceType: ['album'], //從相冊(cè)選擇
success: (res) => {
const tempFilePaths = res.tempFilePaths;
const uploadTask = uni.uploadFile({
url : 'http://47.xxx.xxx.78:8088/chemApp/work/upload.action',
filePath: tempFilePaths[0],
name: 'file',
success: function (uploadFileRes) {
console.log(uploadFileRes);
_this.imgList = [..._this.imgList, uploadFileRes.data]
}
});
}
});
},
ViewImage(e) {
uni.previewImage({
urls: this.imgList,
current: e.currentTarget.dataset.url
});
},
//刪除
DelImg(e) {
uni.showModal({
title: '刪除',
content: '確定要?jiǎng)h除這張圖片?',
cancelText: '取消',
confirmText: '刪除',
success: res => {
if (res.confirm) {
this.imgList.splice(e.currentTarget.dataset.index, 1)
}
}
})
},
}
}
</script>
<style>
@import "../../colorui/main.css";
@import "../../colorui/icon.css";
.cu-form-group .title {
min-width: calc(4em + 15px);
}
</style>
上傳圖片效果

到此這篇關(guān)于uniapp上傳二進(jìn)制圖片的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)uniapp上傳二進(jìn)制圖片內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
JavaScript使用享元模式實(shí)現(xiàn)文件上傳優(yōu)化操作示例
這篇文章主要介紹了JavaScript使用享元模式實(shí)現(xiàn)文件上傳優(yōu)化操作,結(jié)合實(shí)例形式分析了javascript基于享元模式進(jìn)行文件上傳優(yōu)化操作的原理、步驟及相關(guān)使用技巧,需要的朋友可以參考下2018-08-08
關(guān)于在IE下的一個(gè)安全BUG --可用于跟蹤用戶的系統(tǒng)鼠標(biāo)位置
本篇文章小編將為大家介紹,關(guān)于在IE下的一個(gè)安全BUG --可用于跟蹤用戶的系統(tǒng)鼠標(biāo)位置。需要的朋友可以參考一下2013-04-04
談?wù)刯s中的prototype及prototype屬性解釋和常用方法
prototype是javascript中筆記難理解的一部分內(nèi)容,下面通過幾個(gè)關(guān)鍵知識(shí)點(diǎn)給大家講解js中的prototype,對(duì)js中的prototype相關(guān)知識(shí)感興趣的朋友一起學(xué)習(xí)吧2015-11-11
js點(diǎn)擊時(shí)關(guān)閉該范圍下拉菜單之外的菜單方法
下面小編就為大家分享一篇js點(diǎn)擊時(shí)關(guān)閉該范圍下拉菜單之外的菜單方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-01-01
微信小程序按鈕點(diǎn)擊動(dòng)畫效果的實(shí)現(xiàn)
這篇文章主要介紹了微信小程序按鈕點(diǎn)擊動(dòng)畫效果的實(shí)現(xiàn),本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-09-09
OpenLayers3實(shí)現(xiàn)地圖顯示功能
這篇文章主要為大家詳細(xì)介紹了OpenLayers3實(shí)現(xiàn)地圖顯示功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-09-09
Windows Live的@live.com域名注冊(cè)漏洞 利用代碼
Windows Live的@live.com域名注冊(cè)漏洞 利用代碼...2006-12-12

