uniapp小程序上傳文件webapi后端項(xiàng)目asp.net完整代碼
需求
小程序需要上傳用戶相冊圖片或拍攝的照片到后端服務(wù)器
uniapp官方處理小程序文件方法
選擇文件方法:uni.chooseMedia
上傳文件方法:uni.uploadFile
前端代碼
前端項(xiàng)目為vue3類型的uniapp小程序項(xiàng)目
這里封裝一個(gè)簡單的插件來處理圖片的選擇和上傳
<template>
<view class="flex align-start flex-wrap padding-bottom">
<view class="flex flex-direction align-center justify-between margin-left-lg margin-top"
v-for="(item,index) in innerFileList" :key="index">
<image class="cu-avatar xl radius" mode="scaleToFill" :src="item.fileUrl" @tap="previewImg(item)"></image>
<text class='text-second' @tap="handleDelete(item)">刪除圖片</text>
</view>
<view class="cu-avatar xl radius margin-left-lg margin-top" @tap="handleChoose">
<text class="cuIcon-pic"></text>
</view>
</view>
</template>
<script setup>
import {
ref,
computed,
reactive,
onMounted,
watch
} from 'vue'
import {
useStore
} from 'vuex'
import {
toastError,
toastMessage
} from '@/util/common.js'
const props = defineProps({
fileList: Array,
fileUse: Number,
limit: {
type: Number,
default: 5
}
})
const store = useStore()
const emits = defineEmits(['updateFile'])
const token = computed(() => store.state.token)
const innerFileList = ref([])
onMounted(() => {
getFileList()
})
watch(() => props.fileList, (n, o) => {
if (!n || !n.length) {
innerFileList.value = []
} else if (!innerFileList.value.length) {
getFileList()
} else {
if (n.length !== innerFileList.value.length) {
getFileList()
}
}
})
const getFileList = () => {
innerFileList.value = []
if (props.fileList && props.fileList.length) {
for (let item of props.fileList) {
item.fileUrl = getFileUrl(item.fileToken)
}
innerFileList.value = props.fileList
}
}
const {
getFileUrl
} = useGetFileUrl()
// 刪除文件
const handleDelete = item => {
uni.showModal({
title: '確定刪除嗎?',
content: '確定刪除該圖片嗎',
success: res => {
if (res.confirm) {
let index = innerFileList.value.findIndex(x => x.fileUrl === item.fileUrl)
innerFileList.value.splice(index, 1)
}
}
})
}
// 選擇文件
const handleChoose = () => {
if (innerFileList.value.length >= props.limit) {
toastError('不能超過' + props.limit + '張')
return
}
// #ifdef MP-WEIXIN
uni.chooseMedia({
count: 1,
mediaType: ['image'],
fail: error => {
console.log('圖片選擇失敗', error)
},
success: res => {
let file = res.tempFiles[0]
innerFileList.value.push({
id: 0,
fileUrl: file.tempFilePath
})
if (!file) return
handleUpload(file.tempFilePath, '手機(jī)圖片')
}
})
// #endif
// #ifdef APP
uni.chooseImage({
count: 1,
fail: error => {
console.log('圖片選擇失敗', error)
},
success: res => {
let filePath = res.tempFilePaths[0]
innerFileList.value.push({
id: 0,
fileUrl: filePath
})
if (!filePath) return
handleUpload(filePath, '手機(jī)圖片')
}
})
// #endif
}
const handleUpload = (filePath, name) => {
let accessToken = 'Bearer ' + token.value
let uploadUrl = '我的服務(wù)器url'
uni.uploadFile({
url: uploadUrl,
filePath: filePath,
name: name,
header: {
Authorization: accessToken,
},
fail: error => {
console.log('圖片上傳失敗', error)
toastError('圖片上傳失敗')
},
success: uploadRes => {
console.log('圖片上傳成功', uploadRes)
if (uploadRes.statusCode === 200) {
let data = JSON.parse(uploadRes.data)
if (data.data) {
let item = innerFileList.value[innerFileList.value.length - 1]
item.fileId = data.data.picId
item.fileToken = data.data.picToken
item.fileUse = props.fileUse
emits('updateFile', innerFileList.value)
}
}
}
})
}
// 預(yù)覽
const previewImg = item => {
let urls = [item.fileUrl]
uni.previewImage({
urls: urls
})
}
</script>
<style>
</style>后端代碼
后端項(xiàng)目為asp.net6的webapi項(xiàng)目
注意入?yún)镮FormCollection formCollection 和web項(xiàng)目的IFormFile file入?yún)⒂兴鶇^(qū)別
[HttpPost("upload_app_sales_order_cert")]
[Authorize]
public async Task<CommonResponse<UploadFileRes>> UploadSalesOrderCertApp(IFormCollection formCollection)
{
var user = GetUser();
FormFileCollection formFiles = (FormFileCollection)formCollection.Files;
var file = formFiles[0];
//這里換成自己的業(yè)務(wù)邏輯
var res = await _uploadDataService.UploadFileAsync(file, user.UserId, user.DealerId, FileUse.銷售單憑證);
return new CommonResponse<UploadFileRes>(res);
}總結(jié)
到此這篇關(guān)于uniapp小程序上傳文件webapi后端項(xiàng)目asp.net的文章就介紹到這了,更多相關(guān)uniapp小程序上傳文件內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue項(xiàng)目創(chuàng)建并引入餓了么elementUI組件的步驟
這篇文章主要介紹了vue項(xiàng)目創(chuàng)建并引入餓了么elementUI組件的步驟,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2019-04-04
Vue單頁面應(yīng)用中實(shí)現(xiàn)Markdown渲染
這篇文章主要介紹了Vue單頁面應(yīng)用中如何實(shí)現(xiàn)Markdown渲染,幫助大家更好的理解和使用vue,感興趣的朋友可以了解下2021-02-02
vant/vue手機(jī)端長按事件以及禁止長按彈出菜單實(shí)現(xiàn)方法詳解
這篇文章主要介紹了vant/vue手機(jī)端長按事件以及禁止長按彈出菜單實(shí)現(xiàn)方法詳解,需要的朋友可以參考下2022-12-12
electron踩坑之remote of undefined的解決
這篇文章主要介紹了electron踩坑之remote of undefined的解決,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-10-10
vue+elementui 對話框取消 表單驗(yàn)證重置示例
今天小編就為大家分享一篇vue+elementui 對話框取消 表單驗(yàn)證重置示例,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-10-10

