uniapp小程序上傳文件webapi后端項(xiàng)目asp.net完整代碼
需求
小程序需要上傳用戶相冊(cè)圖片或拍攝的照片到后端服務(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)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- uniapp前端實(shí)現(xiàn)微信支付功能全過程(小程序、公眾號(hào)H5、app)
- UNIAPP實(shí)現(xiàn)微信小程序登錄授權(quán)和手機(jī)號(hào)授權(quán)功能(uniapp做微信小程序)
- 前端uniapp微信小程序跨域問題的解決方法
- uniapp實(shí)現(xiàn)微信小程序支付(前端)詳細(xì)代碼
- uniapp微信小程序支付前端生成簽名并調(diào)起微信支付全部代碼
- uniapp微信小程序使用webview嵌套u(yù)niappH5并實(shí)現(xiàn)通信詳細(xì)步驟
- uniapp抖音小程序一鍵獲取用戶手機(jī)號(hào)的示例代碼
相關(guān)文章
Element ui table表格內(nèi)容超出隱藏顯示省略號(hào)問題
這篇文章主要介紹了Element ui table表格內(nèi)容超出隱藏顯示省略號(hào)問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,2023-11-11
vue 自動(dòng)檢測(cè)手機(jī)端響應(yīng)式布局的五種實(shí)現(xiàn)
本文主要介紹了vue自動(dòng)檢測(cè)手機(jī)端響應(yīng)式布局,可以通過結(jié)合 CSS 媒體查詢、Vue 的動(dòng)態(tài)數(shù)據(jù)綁定、適當(dāng)?shù)牡谌綆臁ostCSS 插件以及正確的視口設(shè)置實(shí)現(xiàn),感興趣的可以了解一下2024-07-07
Vue中component標(biāo)簽解決項(xiàng)目組件化操作
這篇文章主要介紹了Vue中component標(biāo)簽解決項(xiàng)目組件化操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-09-09
vue實(shí)現(xiàn)鼠標(biāo)滑動(dòng)展示tab欄切換
這篇文章主要為大家詳細(xì)介紹了vue實(shí)現(xiàn)鼠標(biāo)滑動(dòng)展示tab欄切換,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-04-04
vue通過滾動(dòng)行為實(shí)現(xiàn)從列表到詳情,返回列表原位置的方法
今天小編就為大家分享一篇vue通過滾動(dòng)行為實(shí)現(xiàn)從列表到詳情,返回列表原位置的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-08-08

