uniapp上傳二進制圖片的實現(xiàn)
功能需求:
前端選擇本地文件,將選擇好的文件顯示在界面上進行預(yù)覽,可同時選擇四張進行預(yù)覽。
思路如下:
前端選擇本地的png、jpg、等格式的圖片,將圖片以二進制的形式傳到后端服務(wù)器,后端對二進制圖片進行處理,返回給前端一個服務(wù)器鏈接在線圖片,在瀏覽器就可以打開鏈接訪問的那種。然后前端將這個圖片鏈接渲染在頁面進行預(yù)覽。
首先
我們看一下uniapp的官方文檔:https://uniapp.dcloud.io/api/media/image?id=chooseimage
大概是這樣的
先寫一個模擬的demo
1:首先我是是用了colorUI的框架,在項目里面引入
在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, //默認9
sizeType: ['original', 'compressed'], //可以指定是原圖還是壓縮圖,默認二者都有
sourceType: ['album'], //從相冊選擇
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: '確定要刪除這張圖片?',
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è)置了最多可以選擇四張,圖片鏈接使用了臨時的blob,接下來就要使用后端小伙伴給的接口,將自己本地的二進制文件傳給他了。
在chooseImage選擇好圖片之后,寫一個成功的回調(diào)函數(shù),在回到函數(shù)里面添加一個圖片上傳的方法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]
}
});
}若是請求成功
則返回一個圖片鏈接
添加接口之后 的,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, //默認9
sizeType: ['original', 'compressed'], //可以指定是原圖還是壓縮圖,默認二者都有
sourceType: ['album'], //從相冊選擇
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: '確定要刪除這張圖片?',
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上傳二進制圖片的實現(xiàn)的文章就介紹到這了,更多相關(guān)uniapp上傳二進制圖片內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
JavaScript使用享元模式實現(xiàn)文件上傳優(yōu)化操作示例
這篇文章主要介紹了JavaScript使用享元模式實現(xiàn)文件上傳優(yōu)化操作,結(jié)合實例形式分析了javascript基于享元模式進行文件上傳優(yōu)化操作的原理、步驟及相關(guān)使用技巧,需要的朋友可以參考下2018-08-08
關(guān)于在IE下的一個安全BUG --可用于跟蹤用戶的系統(tǒng)鼠標位置
本篇文章小編將為大家介紹,關(guān)于在IE下的一個安全BUG --可用于跟蹤用戶的系統(tǒng)鼠標位置。需要的朋友可以參考一下2013-04-04
談?wù)刯s中的prototype及prototype屬性解釋和常用方法
prototype是javascript中筆記難理解的一部分內(nèi)容,下面通過幾個關(guān)鍵知識點給大家講解js中的prototype,對js中的prototype相關(guān)知識感興趣的朋友一起學習吧2015-11-11
Windows Live的@live.com域名注冊漏洞 利用代碼
Windows Live的@live.com域名注冊漏洞 利用代碼...2006-12-12

