uniapp上传图片
作者:互联网
getUploadImg(e) {
uni.chooseImage({
count: 9,
// 默认9
sizeType: ['compressed'],
sourceType: ['album', 'camera'],
success: (res) => {
// 图片的本地临时文件路径列表
this.tempFilePaths = res.tempFilePaths;
this.fileData=res.tempFiles
//imgList为渲染页面的图片数组
if (this.imgList.length == 0) {
this.imgList = this.tempFilePaths
} else {
this.imgList = this.imgList.concat(this.tempFilePaths)
}
}
});
},
2、
//上传单张图片
var token = 'Bearer ' + uni.getStorageSync('token');
uni.uploadFile({ //循环调用上传单个文件的接口,实现多文件上传
url: common.apiUrl +'/elms/devicerelationpicture', //仅为示例,非真实的接口地址
name: 'files', //后台接收图片的字段,自定义的
fileType:'image',
filePath: this.tempFilePaths[0],
formData: {
'relationId':this.relationId,
'type':'device',
},
header:{
'Authorization': token,
'Token': token,
},
success: function (uploadFileRes) {
} ,
})
3、
//上传多张图片
var token = 'Bearer ' + uni.getStorageSync('token');
for (let i = 0; i < this.fileData.length; i++) {
let obj = new Object();
obj.name = 'files'; //后台接收图片的字段,自定义的
obj.uri = this.fileData[i].path;
this.fileList.push(obj);
}
uni.uploadFile({ //循环调用上传单个文件的接口,实现多文件上传
url: common.apiUrl +'/elms/devicerelationpicture', //仅为示例,非真实的接口地址
files:this.fileList, //这里传递的参数是files,不是file.且不需要传name和filePath字段
formData: {
'relationId':this.relationId,
'type':'device',
},
header:{
'Authorization': token,
'Token': token,
},
success: function (uploadFileRes) {
} ,
})
},
4、控制台
标签:files,uniapp,tempFilePaths,imgList,token,relationId,uni,上传,图片 来源: https://www.cnblogs.com/panzai/p/16614392.html