uniapp 实现多个图片上传


uniapp 实现多个图片上传(需用 uniCould)


前言

在用uniapp 编写程序时,遇到上传图片问题,可以使用 uniCould 的API uploadFile() 但是该接口一次只能上传一张图片,当需要上传多张照片时,就需要在其基础上进行改动,本文就是用自己的理解实现多张图片上传问题


提示:以下是本篇文章正文内容,下面案例可供参考

一、uploadFile() 是什么?

uploadFile(Object object)
直接上传文件到云存储。

客户端上传文件到云函数、云函数再上传文件到云存储,这样的过程会导致文件流量带宽耗费较大。所以一般上传文件都是客户端直传。

官方文档 [https://uniapp.dcloud.io/uniCloud/storage?id=gettempfileurl]

二、使用步骤

<button type="default" @click="chooseImages" >上传</button>
<button type="default" @click="getImages" >下载链接</button>

1.上传图片

chooseImages(){
				// 上传图片
				uni.chooseImage({
					count: 9,
					success: (res) => {
						// console.log(res);
						// 获取每个图片的本地上传地址
						this.getPic(res.tempFilePaths)},

2.获取每个图片的上传地址

async getPic(picUrl) {	
			// 用循环遍历每一个上传图片的本地地址
				for (var i = 0; i < picUrl.length; i++) {
					// console.log(picUrl[i]);
			// 将本地地址传入服务器,实现图片上传
					await uniCloud.uploadFile({
						filePath: picUrl[i],
						// 上传图片名字可自己定义
						cloudPath: i + '.jpg',
						onUploadProgress:function(progressEvent){
							var percentCompleted = Math.round((progressEvent.loaded * 100) / progressEvent.total);
						}
					}).then(res => {
						// console.log(res)
						// imagesUrl 在 data 里定义的接收 图片地址 的数组
						this.imagesUrl.push(res.fileID) ;
					})
				}
			// 上传后的服务器图片地址	
			console.log(this.imagesUrl);
			},

3.获取每个图片的可下载地址

getImages(imagesUrl) {
				uniCloud.getTempFileURL({
					fileList: this.imagesUrl,
				}).then(res => {
					// res.fileList 图片相关信息的数组
					console.log(res.fileList);
				})
			}

总结

以上就是今天要讲的内容,本文仅仅是自己的一点拙见,欢迎大家评论,希望能够帮助到和我一样的前端小白。

实现多个图片的上传和保存,你需要使用uniapp的上传组件和网络请求API。 首先,在模板中定义一个上传组件,例如: ```html <template> <div> <div v-for="(item, index) in images" :key="index"> <img :src="item" width="100" height="100" /> </div> <button @click="uploadImages">上传图片</button> <button @click="saveImages">保存图片</button> </div> </template> <script> export default { data() { return { images: [] // 存放上传的图片 }; }, methods: { uploadImages() { uni.chooseImage({ count: 9, sizeType: ["original", "compressed"], sourceType: ["album", "camera"], success: res => { this.images = res.tempFilePaths; } }); }, saveImages() { if (this.images.length === 0) { uni.showToast({ title: "请先上传图片", icon: "none" }); return; } let formData = new FormData(); for (let i = 0; i < this.images.length; i++) { formData.append("file", this.images[i]); } uni.request({ url: "http://your_upload_url", method: "POST", header: { "content-type": "multipart/form-data" }, data: formData, success: res => { if (res.statusCode === 200) { uni.showToast({ title: "上传成功", icon: "success" }); } else { uni.showToast({ title: "上传失败", icon: "none" }); } } }); } } }; </script> ``` 上述代码中,我们使用了uni.chooseImage方法来选择多张图片,并将它们存放在images数组中。然后,我们使用FormData将images数组中的图片打包成一个form表单,使用uni.request方法将其上传到服务器。 如果要保存图片,你需要使用canvas将图片绘制到画布上,并使用uni.saveImageToPhotosAlbum将画布保存到相册中。例如: ```html <template> <div> <canvas ref="canvas" width="300" height="300"></canvas> <button @click="saveCanvas">保存画布</button> </div> </template> <script> export default { methods: { saveCanvas() { uni.showLoading({ title: "保存中...", mask: true }); let canvas = this.$refs.canvas; let ctx = canvas.getContext("2d"); let image = new Image(); image.src = "http://your_image_url"; image.crossOrigin = "anonymous"; image.onload = () => { ctx.drawImage(image, 0, 0, 300, 300); uni.canvasToTempFilePath({ canvas: canvas, success: res => { uni.saveImageToPhotosAlbum({ filePath: res.tempFilePath, success: () => { uni.hideLoading(); uni.showToast({ title: "保存成功", icon: "success" }); }, fail: () => { uni.hideLoading(); uni.showToast({ title: "保存失败", icon: "none" }); } }); } }); }; } } }; </script> ``` 上述代码中,我们使用canvas绘制了一张图片,然后使用uni.canvasToTempFilePath将画布转换为临时文件路径,最后使用uni.saveImageToPhotosAlbum将图片保存到相册中。 注意:在使用uni.saveImageToPhotosAlbum保存图片时,需要用户授权,否则会保存失败。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值