实现效果:

pPEZRI0.png

2023-4-13 修改 []传参数组之对h5有用 app端只取到一张图片

1
2
3
4
5
6
7
8
9
<u-form-item :label-position="labelPosition" label="上传图片" prop="photo" label-width="150">
<u-upload width="160" height="160" :auto-upload="false" @on-list-change="imageListChange"
:source-type="['camera']" ref="uUpload" :custom-btn="true">
<view slot="addBtn" class="slot-btn" hover-class="slot-btn__hover" hover-stay-time="150">
<u-icon name="camera" size="60" color="#606266"></u-icon>
<span class="text">拍照上传</span>
</view>
</u-upload>
</u-form-item>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
imageListChange(lists, name) {
this.paramObj.images = []
if (lists.length > 0) {
lists.forEach((r, index) => {
r.file.uri = r.file.path
let obj = {}
// obj.name = 'images[]'; app端无效
obj.name = 'image'+index;
//默认的name是file,后端接口写的是images 不加[],选择多张图片时会只传一张
obj.uri = r.file.path;
// uni.uploadFile(OBJECT) files参数格式为[{name:'',file:File,uri :'必需项'}]
this.paramObj.images.push(obj)
})
} else {
this.paramObj.images = []
}
},
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
//携带其他参数上传
uni.uploadFile({
url: 'xxx',
files: this.paramObj.images,
name: 'images[]', //文档里说files时 name无效
formData: {
'p_type': this.paramObj.p_type,
'wo_number': this.paramObj.wo_number
},
header: {},
success: (res) => {
console.log(res);
},
fail(error) {
console.log(error);
},
complete() {
uni.hideLoading()
}
});