wx.chooseImage({ success: function (res) { var tempFilePaths = res.tempFilePaths//确定是一个多图的数组 wx.uploadFile({ url: 'http://192.168.11.168/kljjd/home/report/add/' , filePath: tempFilePaths, //这里是多个不行, tempFilePaths[0]这样可以 name: 'file[]' , //我尝试这样写也不行 formData:{ 'user' : 'test' }, success: function (res){ util.debug(res); } }) } }) //控制台提示 WAService.js:1 uploadFile:fail parameter error: parameter.filePath should be String instead of Array; |
别告诉我要循环多次一张张的上传啊?
// 多图上传
choosePhoto(e){
var
that =
this
;
wx.chooseImage({
count: 6,
// 默认9
sizeType: [
'original'
,
'compressed'
],
// 可以指定是原图还是压缩图,默认二者都有
sourceType: [
'album'
,
'camera'
],
// 可以指定来源是相册还是相机,默认二者都有
success:
function
(res) {
// 返回选定照片的本地文件路径列表,tempFilePath可以作为img标签的src属性显示图片
var
len = res.tempFilePaths.length;
for
(
var
i=0;i<len;i++){
that.data.picLists.push(res.tempFilePaths[i])
}
var
imgs = that.data.picLists.length;
console.log(imgs)
if
(imgs >= 6) {
that.data.aShow =
true
;
that.data.picLists.length = 6
}
else
{
that.data.aShow =
false
;
}
that.setData({
picLists: that.data.picLists,
tShow:
true
,
aShow: that.data.aShow
})
console.log(that.data.picLists)
}
})
},
wxml 怎么写的
<view class=
'video-item photo-item'
hidden=
"{{currentTab!=1}}"
>
<form bindsubmit=
'formSubmitPhoto'
>
<textarea maxlength=
'50'
name=
'post_desc'
placeholder=
'随时分享新鲜事'
></textarea>
<view class=
'upload-view'
>
<view class=
"img-box {{tShow == true ? 'active':''}}"
wx:
for
=
"{{picLists}}"
wx:key=
""
>
<image src=
'{{item}}'
data-id=
"{{index}}"
mode=
'aspectFit'
bindtap=
'previewImg'
></image>
<icon type=
'clear'
size=
'20'
data-index=
"{{index}}"
color=
'red'
bindtap=
'clearPhoto'
></icon>
</view>
<view class=
"uv-box {{aShow == true ? 'active':''}}"
bindtap=
'choosePhoto'
>
<view><text>+</text>上传图片</view>
</view>
</view>
<button class=
'post-btn'
formType=
"submit"
>发表</button>
</form>
</view>
public
function
done()
{
$user_id
=
intval
(
$_POST
[
'user_id'
]);
if
(!
$user_id
) {
echo
json_encode(
array
(
'status'
=>0,
'err'
=>
'登录状态异常.'
));
exit
();
}
//多图存储 记录方式为url,其中name是图片名称
$image
=
array
();
if
(!
empty
(
$_POST
[
'image'
])){
//{"url":"http:\/\/127.0.0.1\/dyxcx\/public\/upload\/default\/20180728\/60da1053c6127f54fee5498a7e7557c5.jpg","name":"60da1053c6127f54fee5498a7e7557c5.jpg"}
$img
=
explode
(
','
,
$_POST
[
'image'
]);
foreach
(
$img
as
$k
=>
$v
) {
if
(preg_match(
'/(http:\/\/)|(https:\/\/)/i'
,
$v
)){
$image
[
$k
][
'url'
]=
$v
;
}
else
{
$img1
=
explode
(
"/upload/"
,
$v
);
$image
[
$k
][
'url'
]=__DATAURL__.
$img1
[1];
}
$name1
=
explode
(
"/upload/"
,
$v
);
$name2
=
explode
(
"/"
,
$name1
[1]);
$image
[
$k
][
'name'
]=
$name2
[2];
}
$image
= json_encode(
$image
);
}
else
{
$image
=
""
;
}
$data
= [
'user_id'
=>
$user_id
,
'thumbnail'
=>
$image
,
'post_desc'
=>
$_POST
[
'post_desc'
],
'published_time'
=>time(),
];
$res
= DB::name(
'portal_post'
)->insert(
$data
);
if
(
$res
){
echo
json_encode(
array
(
'status'
=>1,
'arr'
=>
$data
));
exit
;
}
else
{
echo
json_encode(
array
(
'status'
=>0,
'err'
=>
"添加失败"
));
exit
;
}
}
// 准备上传图片
wx.uploadFile({
url: app.d.ceshiUrl +
'/Api/Profile/photo'
,
filePath: that.data.tempFile,
name:
'file'
,
formData: {
'user_id'
: user_id
},
header: {
"Content-Type"
:
"multipart/form-data"
},
success:
function
(res) {
var
info = e.detail.value;
//表单里的所有值
var
photo = JSON.parse(res.data);
info.tempFile = photo.code;
if
(photo.status == 0) {
wx.showToast({
title: photo.err,
duration: 2000,
icon:
'none'
});
return
false
;
}
// 确认提交
wx.request({
url: app.d.ceshiUrl +
'/Api/Profile/addPost'
,
method:
'POST'
,
data: info,
header: {
'content-type'
:
'application/x-www-form-urlencoded'
// 默认值
},
success:
function
(res) {
// console.log(res.data);
// console.log(info);
var
data = res.data.arr;
that.setData({
user_id :user_id,
post_desc:data.post_desc,
tempFile: data.tempFile,
checkbox: data.cate_id
});
wx.showToast({
title:
"发布成功"
,
duration: 2000,
icon:
'success'
});
return
false
;
},
fail:
function
() {
wx.showToast({
title:
'请填写正确'
,
duration: 2000,
icon:
'none'
});
return
false
;
}
})
}
});
启动事件如下:
formSubmitPhoto
:
function
(e){
wx.uploadFile({
wx.request({....})
})
}
异步循环有点麻烦,直接递归吧
怎么递归的?串行的还是并行的?文档说可以最大并行10 怎么操作串行还是并行?
递归暂时只能这样做了
建议你循环调用,我是这么解决了
在success里面再去调用上传,安卓要多次调用
回复3楼: 昨天刚遇到这个问题,formData传的参数需要encodeURI编码,服务端再解码。
1楼:是怎么循环解决的,方便贴出来看看吗?
回复3楼:我没遇到过这个问题,好像在百度上看到有人说过你这个问题,
谢谢大家啊,我估计也有只能循环了
我按照上面写的代码,上传一张图片的话,苹果系统可以上传成功,但是android系统上传失败,提示fail:unknown,大神遇到过么