//util.js
function uploadFile(path,imgList){
	uni.showLoading({
		title: '上传中'
	});
	uni.uploadFile({
		url:'上传接口地址',
		filePath:path,
		name: 'file',
		success: res => {
			let temp = JSON.parse(res.data)
			imgList.push(temp.data)
		},
		file: res => {
			uni.hideLoading()
		}
	})
}
module.exports = {
	uploadFile:uploadFile
}

在全局main.js使用util.js

import util from "./common/util.js";
Vue.prototype.util = util;
<view class="p-20">
	<text>头像</text>
	<view class="solid-bottom" @tap="selectImg">上传头像</view>
</view>
selectImg() {
	uni.chooseImage({
		count: 1, //默认9
		sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图,默认二者都有
		sourceType: ['album'], //从相册选择
		success: (res) => {
			this.util.uploadFile(res.tempFiles[0].path,this.imgList)
		}
	})
},
Logo

欢迎加入 MCP 技术社区!与志同道合者携手前行,一同解锁 MCP 技术的无限可能!

更多推荐