APICloud 社区api.ajax接口多个文件上传怎么传参

如题所述

第1个回答  2016-08-22
通过post请求上传,有两种方式,跟form表单通过file标签提交文件是一样的。
1、一种是键值对的方式,也就是一个name对应一个file:
api.ajax({
url: 'http://host/upLoad',
method: 'post',
report:true,//回调上传进度
data: {
files: {
file1: '/sdcard/a.png',
file2: '/sdcard/b.png',
file3: '/sdcard/c.png',
}
}
}, function(ret, err) {
if (ret) {
console.log(JSON.stringify(ret));
} else {
console.log(JSON.stringify(err));
}
});

2、另外一种是上传文件数组,这种方式你的服务器要配合做特殊的处理进行支持:
api.ajax({
url: 'http://host/upLoad',
method: 'post',
report:true,//回调上传进度
data: {
files: {
images: ['/sdcard/a.png', '/sdcard/b.png', '/sdcard/c.png']
}
}
}, function(ret, err) {
if (ret) {
console.log(JSON.stringify(ret));
} else {
console.log(JSON.stringify(err));
}
});