<template>
	<view class="content">
		<button @click="camera">点击</button>
	</view>
</template>

<script>
	export default {
		methods: {
			camera() {
				let imgCount = 3
				uni.chooseImage({
					count: imgCount,
					sizeType: ['compressed'],
					sourceType: ['album'],
					success: (res) => {
						const files = res.tempFiles || []
						let filesList = []
						files.forEach((item, index) => {
							filesList.push({
								name: 'file',
								uri: res.tempFilePaths[index],
								file: item
							})
						})

						let body = {
							"uploadUser": "张三",
							"uploadTime": "2023-06-03 22:06:52",
						}

						uni.uploadFile({
							url: '请求地址',
							methods: "POST",
							files: filesList,
							formData: {
								body: JSON.stringify(body)
							},
							header: {
								"userid": "xxx",
								"token": "xxx",
							},
							success: res => {
								uni.showToast({
									title: '成功'
								})
							},
							fail: res => {
								console.log(res)
								uni.showToast({
									title: '失败'
								})
							},
							complete: res => {}
						})
					}
				})
			},
		}
	}
</script>

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐