欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

uniapp小程序上傳文件webapi后端項(xiàng)目asp.net完整代碼

 更新時(shí)間:2024年07月22日 09:58:35   作者:woflyoycm  
在uniapp中,實(shí)現(xiàn)文件上傳功能也變得非常簡(jiǎn)單,下面這篇文章主要給大家介紹了關(guān)于uniapp小程序上傳文件webapi后端項(xiàng)目asp.net的相關(guān)資料,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下

需求

小程序需要上傳用戶相冊(cè)圖片或拍攝的照片到后端服務(wù)器

uniapp官方處理小程序文件方法

選擇文件方法:uni.chooseMedia

uni-app官網(wǎng)

上傳文件方法:uni.uploadFile

uni.uploadFile(OBJECT) 

前端代碼

前端項(xiàng)目為vue3類型的uniapp小程序項(xiàng)目

這里封裝一個(gè)簡(jiǎn)單的插件來(lái)處理圖片的選擇和上傳

<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('不能超過(guò)' + 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)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Element ui table表格內(nèi)容超出隱藏顯示省略號(hào)問(wèn)題

    Element ui table表格內(nèi)容超出隱藏顯示省略號(hào)問(wèn)題

    這篇文章主要介紹了Element ui table表格內(nèi)容超出隱藏顯示省略號(hào)問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,
    2023-11-11
  • vuex 的簡(jiǎn)單使用

    vuex 的簡(jiǎn)單使用

    vuex是一個(gè)專門為vue.js設(shè)計(jì)的集中式狀態(tài)管理架構(gòu)。這篇文章主要介紹了vuex 的簡(jiǎn)單使用,需要的朋友可以參考下
    2018-03-03
  • uniapp和vue的區(qū)別詳解

    uniapp和vue的區(qū)別詳解

    這篇文章主要介紹了uniapp和vue的區(qū)別,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2021-10-10
  • vue 自動(dòng)檢測(cè)手機(jī)端響應(yīng)式布局的五種實(shí)現(xiàn)

    vue 自動(dòng)檢測(cè)手機(jī)端響應(yīng)式布局的五種實(shí)現(xiàn)

    本文主要介紹了vue自動(dòng)檢測(cè)手機(jī)端響應(yīng)式布局,可以通過(guò)結(jié)合 CSS 媒體查詢、Vue 的動(dòng)態(tài)數(shù)據(jù)綁定、適當(dāng)?shù)牡谌綆?kù)、PostCSS 插件以及正確的視口設(shè)置實(shí)現(xiàn),感興趣的可以了解一下
    2024-07-07
  • Vue中component標(biāo)簽解決項(xiàng)目組件化操作

    Vue中component標(biāo)簽解決項(xiàng)目組件化操作

    這篇文章主要介紹了Vue中component標(biāo)簽解決項(xiàng)目組件化操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-09-09
  • VUE axios上傳圖片到七牛的實(shí)例代碼

    VUE axios上傳圖片到七牛的實(shí)例代碼

    本篇文章主要介紹了VUE axios上傳圖片到七牛的實(shí)例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-07-07
  • vue實(shí)現(xiàn)鼠標(biāo)滑動(dòng)展示tab欄切換

    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-cli在ie9+中無(wú)效的問(wèn)題

    快速解決vue-cli在ie9+中無(wú)效的問(wèn)題

    今天小編就為大家分享一篇快速解決vue-cli在ie9+中無(wú)效的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2018-09-09
  • vue中插槽整理及用法分析

    vue中插槽整理及用法分析

    在本篇文章里小編給大家整理的是一篇關(guān)于vue中插槽整理及用法分析內(nèi)容,對(duì)此有興趣的朋友們可以跟著學(xué)習(xí)下。
    2021-12-12
  • vue通過(guò)滾動(dòng)行為實(shí)現(xiàn)從列表到詳情,返回列表原位置的方法

    vue通過(guò)滾動(dòng)行為實(shí)現(xiàn)從列表到詳情,返回列表原位置的方法

    今天小編就為大家分享一篇vue通過(guò)滾動(dòng)行為實(shí)現(xiàn)從列表到詳情,返回列表原位置的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2018-08-08

最新評(píng)論