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

后台使用的是Java SSM框架

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.do")
public void doPost(HttpServletRequest request, @RequestParam("file") MultipartFile file, HttpServletResponse response)
		throws IOException {
	if (file != null && !file.isEmpty()) {
		try {
			String targetSrc = request.getServletContext().getRealPath("/")+"../file";
			String fileName = file.getOriginalFilename();
			fileName = fileName.substring(fileName.lastIndexOf("."));
			fileName = UUID.randomUUID().toString() + fileName;
			File targetFile = FileUtil.Upload(file, targetSrc, fileName);
			System.out.println(targetFile.getPath());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

 

你可能感兴趣的:(uni-app)