简述:在写完若依后端的上传文件到服务器的接口后,Ruoyi框架上传文件、图片到阿里云Oss-CSDN博客,需要在移动端(uniapp)上同步操作。使用uni-file-picker组件完成。
上图:
1、引用组件:
组件地址:uni-file-picker 文件选择上传 - DCloud 插件市场
引用组件后,配置好对应的属性,
<uni-file-picker style="height: 150rpx;"
v-model="actData.actSurfacePlot" title="选择您的活动封面" limit="3"
file-mediatype="image" @select="upload" :auto-upload="false">
</uni-file-picker>
<!-- title:就是提示用户的词语; v-model:用于绑定你用于存储选择的文件的属性;
limit:限制最大文件数;file-mediatype="",用于指定为image或file;
@select表示选中后,进行的函数-->
2、补充JS函数:
upload(e) {
const tempFilePaths = e.tempFilePaths;
uni.uploadFile({
url: 'http://localhost:80/dev-api/common/upload',
//你的上传文件的接口地址:若依的一般就是这个
filePath: tempFilePaths[0],
name: 'file',
formData: {
'user': 'test'
},
success: (uploadFileRes) => {
// console.log(uploadFileRes.data);
const imgUrls = JSON.parse(uploadFileRes.data).url;
this.selectedImg = this.selectedImg + imgUrls+',' ;
//this.selectedImg是我定义的获取文件的属性,每个用逗号隔开。
console.log(this.selectedImg);
}
});
}