uni-app 拍照或从相册上传图片

本文介绍了一个小程序中实现图片上传功能的方法,详细展示了如何使用JavaSSM框架在后台进行图片接收和处理。文章包括了小程序前端的代码示例,以及Java后台的图片上传控制器代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

后台使用的是Java SSM框架

<template>
	<view>
		<view class="cu-bar bg-white margin-top">
			<view class="action">
				图片上传
			</view>
			<view class="action">
				{{imgList.length}}/{{imgMaxNum}}
			</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>
		<view class="cu-form-group upload-btn">
			<button class="cu-btn block bg-blue margin-tb-sm lg">上传</button>
		</view>
	</view>
</template>
var _this = this;
export default {
	data() {
		return {
			imgList: [],
			imgMaxNum: 4
		}
	},
	onLoad() {

	},
	methods: {
		ChooseImage() {
			_this = this;
			uni.chooseImage({
				count: 4, //默认9
				sizeType: ['original'], //可以指定是原图还是压缩图,默认二者都有
				sourceType: ['camera','album'], //从相机、相册选择
				success: (res) => {
					var tempFilePaths = res.tempFilePaths;
					if (_this.imgList.length+tempFilePaths.length > _this.imgMaxNum) {
						uni.showToast({
							title: '超过上传图片的最大数量',
							icon: 'none'
						})
					} else {
						if (_this.imgList.length != 0) {
							_this.imgList = _this.imgList.concat(res.tempFilePaths);
						} else {
							_this.imgList = res.tempFilePaths;
						}
						for (var i = 0; i < tempFilePaths.length; i++) {
							uni.uploadFile({
								url: baseUrl + "uploadImgAPP.do",
								filePath: tempFilePaths[i],
								name: "file", // 一定要与后台@RequestParam("file") MultipartFile变量名一致
								success(res) {
									console.log(res);
								}
							});
						}
					}
				}
			});
		},
		ViewImage(e) {
			uni.previewImage({
				urls: this.imgList,
				current: e.currentTarget.dataset.url
			});
		},
		DelImg(e) {
			this.imgList.splice(e.currentTarget.dataset.index, 1)
		},
	}
}
@RequestMapping("uploadImgAPP")
public String uploadImgAPP(@RequestParam("file") MultipartFile file,HttpServletRequest request){
	String fileName = "";
	if (file != null && !file.isEmpty()) {
		try {
			String targetSrc = request.getServletContext().getRealPath("/")+"files";
			fileName = file.getOriginalFilename();
			fileName = fileName.substring(fileName.lastIndexOf("."));
			fileName = UUID.randomUUID().toString() + fileName;
			File targetDir = new File(targetSrc);
			if (!targetDir.exists()) {
				targetDir.mkdirs();
			}
			File targetFile = new File(targetSrc, fileName);
			if (targetFile.exists()) {
				targetFile.delete();
			}
			file.transferTo(targetFile);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	return fileName;
}

 

评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值