跳到主要内容

上传通用方法

wxml

<!-- key 上传列表名称 -->

<t-upload
mediaType="{{['video','image']}}"
max="{{12}}"
files="{{inspectionReportList}}"
data-key="inspectionReport"
bind:add="handleAdd"
bind:remove="handleRemove"
>
<t-button
theme="primary"
size="large"
bind:tap="handleUpload"
data-key="inspectionReport"
>上传</t-button
>
</t-upload>

js

{
fileList:[]
}

handleAdd(e) {
const fileList = this.data[`${e.currentTarget.dataset.key}List`];

const { files } = e.detail;

// 方法1:选择完所有图片之后,统一上传,因此选择完就直接展示

this.setData({

[`${e.currentTarget.dataset.key}List`]: [...fileList, ...files], // 此时设置了 fileList 之后才会展示选择的图片

});

console.log([`${e.currentTarget.dataset.key}List`]);

// 方法2:每次选择图片都上传,展示每次上传图片的进度

// files.forEach(file => this.uploadFile(file))

},
handleRemove(e) {

const { index } = e.detail;

// const { fileList } = this.data;

const fileList = this.data[`${e.currentTarget.dataset.key}List`];

fileList.splice(index, 1);

this.setData({

[`${e.currentTarget.dataset.key}List`]:fileList,

});

},
/**

* 处理上传的文件

*/

async handleUpload(e){

try {

let {key} = e.currentTarget.dataset;

let fileList = this.data[`${key}List`];

console.log(fileList);

let res = await uploadMultipleFiles({fileList});

console.log(res);

} catch (error) {

console.error(error);

}

},