uniapp实现图片上传功能:


1、接口详情:

在这里插入图片描述

2、uni-file-picker上传:

【1】导入uni-file-picker插件

【2】template

<view class="example-body">
		<uni-file-picker title="上传营业执照" limit="3" fileMediatype="image" mode="grid" 		
		v-model="imageList" @select="selectFileFun($event)" @delete="pickerDelete($event)"
		:sizeType="sizeType" :imageStyles="imageStyles" :auto-upload="false" /> 					 
</view>

【3】script

export default {
	data() {
		return {
			imageList: [],
			sizeType: ['compressed'], //设置图片压缩
			imageStyles: {
				width: 120,
				height: 120,
			},
		}
	},
	methods:{
		//选择图片
		selectFileFun(e) {
			e.tempFilePaths.map(item => {
				this.imageList.push({
					name: 'imageList',
					uri: item
				})
			})
		},
		pickerDelete(e) {
			this.imageList.map((item, i) => {
				if (item.uri == e.tempFilePath) {
					this.imageList.splice(i, 1)
				}
			})
		},
	}
}
3、uni.chooseImage选择图片
uni.chooseImage({
	count: 1, //默认9
	sizeType: ['original', 'compressed'],//设置图片压缩compressed,原始图片original
	sourceType: ['album', 'camera'],
	success: (res) => {
		console.log(res.tempFilePaths,'图片的本地文件路径列表');
	}
});
4、图片压缩:

https://editor.csdn.net/md/?articleId=126724165

/**
* H5端图片压缩
*  参数说明:
*  imgSrc 图片url
*  scale缩放比例 0-1
*  返回base64
*  callback 回调设置返回值 
*/
translate(imgSrc, scale, callback) {
	var img = new Image();
	img.src = imgSrc;
	img.onload = function() {
		var that = this;
		var h = that.height; // 默认按比例压缩
		var w = that.width;
		var canvas = document.createElement('canvas');
		var ctx = canvas.getContext('2d');
		var width = document.createAttribute("width");
		width.nodeValue = w;
		var height = document.createAttribute("height");
		height.nodeValue = h;
		canvas.setAttributeNode(width);
		canvas.setAttributeNode(height);
		ctx.drawImage(that, 0, 0, w, h);
		var base64 = canvas.toDataURL('image/jpeg', scale); //压缩比例
		canvas = null;
		callback(base64);
	}
},
//选择图片、视频
selectFileFun(e) {
	const that = this
	console.log('压缩前', e.tempFiles[0].size)
	e.tempFilePaths.forEach(item => {
		that.imageList.push({
			name: 'imageList',
			uri: imgUrl
		})
	})
},
//上传
uploadFile(){
	const that = this
	if (this.imageList.length > 0) {
		this.imageList.forEach(item => {
			that.translate(item.uri, 0.5, imgURL => {
				//查看压缩后的大小
				uni.getFileInfo({
					filePath: imgUrl,
					success: imgInfo => {
						console.log('压缩后', imgInfo.size);
					}
				})
				item.uri = imgURL
			})
		})
	}
	...
}

在这里插入图片描述

5、文件上传:
uni.uploadFile({
	url: url,//上传图片的地址
	filePath: ...,//这里是图片的本地文件路径列表(选择图片成功的时候可以拿到,在上边的success回调中res.tempFilePaths即可拿到)
	name: '',//上传的名字叫啥都行
	headers: {
		accessToken: //可以设置你的请求头的token噢
	},
	success(res) {
		//上传成功的回调
		这个res是后端返回给你上传成功的数据里边一般会有上传之后图片的在线路径
	}
})

或者

uni.uploadFile({
	url: url, 
	files: flieList,//flieList图片等
	formData: param,//其它上传参数
	header: {
		"token": uni.getStorageSync('Token')
	},
	success: (res) => {
		console.log(res)
	},
	fail: error => {
		uni.showModal({
			title: '提示',
			content: error,
			showCancel: false
		});
		uni.hideLoading();
	},
	complete: () => {
		uni.hideLoading();
	}
});
  • 8
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
UniApp 是一个基于 Vue.js 的跨平台应用框架,可以用于开发 iOS、Android、Web、小程序等多个平台的应用。在 UniApp实现拍照上传图片功能可以通过使用原生的拍照组件和文件上传组件来实现。 首先,你需要引入 UniApp 的拍照组件 `camera`,在页面中添加一个按钮,用于触发拍照操作。当用户点击按钮时,调用拍照组件,拍摄照片。 然后,你可以使用 UniApp 的文件上传组件 `upload` 将拍摄的照片上传到服务器。你需要设置上传的文件路径、文件名称等相关参数,并监听文件上传的回调函数,以获取上传成功后的结果。 以下是一个简单的示例代码: ```html <!-- 在页面中添加一个拍照按钮 --> <template> <view> <button @click="takePhoto">拍照</button> </view> </template> <script> export default { methods: { takePhoto() { uni.chooseImage({ count: 1, // 可选图片的数量 sizeType: ['original', 'compressed'], // 可选原图和压缩图 sourceType: ['camera'], // 只能拍照,不允许从相册选择 success: (res) => { const tempFilePaths = res.tempFilePaths // 获取拍摄的照片临时文件路径 // 使用文件上传组件进行图片上传 uni.uploadFile({ url: '上传图片的接口地址', filePath: tempFilePaths[0], // 选择的图片的临时文件路径 name: 'file', // 上传文件对应的 key 名称 formData: {}, // 可以额外传递的参数 success: (res) => { console.log('上传成功', res.data) // 在这里可以根据返回结果进行相应处理 }, fail: (err) => { console.log('上传失败', err) // 在这里可以根据错误信息进行相应处理 } }) }, fail: (err) => { console.log('拍照失败', err) // 在这里可以根据错误信息进行相应处理 } }) } } } </script> ``` 以上代码中,你需要将 `'上传图片的接口地址'` 替换为你的服务器端接口地址,用于接收图片并保存到服务器。另外,你还可以根据实际需求添加错误处理和文件上传进度等相关逻辑。 希望以上信息对你有所帮助!如果还有其他问题,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Sun Peng

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值