多张图片上传

假设图片路径保存在数组pic中,在上传触发事件中

uploadImage(pic[0], 0);
api.addEventListener({ //上传图片完成后进行数据储存
    name: 'pic_ok'
}, function(ret, err) {

        });
function uploadImage(url, Index) {
        //压缩图片
lrz(url)
    .then(function(rst) {
        var path = rst.base64.replace('data:image/jpeg;base64,', '');
        var trans = api.require('trans');
        var cacheDir = api.cacheDir;
        trans.saveImage({
            base64Str: path,
            album: false,
            imgPath: cacheDir + '/upload/',
            imgName: Index + 'temp.png'
        }, function(ret, err) {
            if (ret.status) {
                var item = cacheDir + '/upload/' + Index + 'temp.png';
                model.uploadFile({
                            data: {
                                file: {
                                    name: Index + 'temp.png',
                                    url: item
                                },
                            }
                        }, function(ret, err) {
                            if (ret.url) {
                                Pic_url = ret.url;
                                pic[Index] = Pic_url;
                                var fs = api.require('fs');
                                fs.remove({
                                    path: cacheDir + '/upload/' + Index + 'temp.png'
                                }, function(ret, err) {
                                    var status = ret.status;
                                    if (status) {
                                        if (Index != (pic.length - 1)) {
                                        Index++;
                                        uploadImage(pic[Index], Index);
                                        } else {        
                                                              api.sendEvent({
                                                                name: 'pic_ok',
                                                            })
                                                            api.hideProgress();
                                                        }
                                                    });
                                    } else {
                                        api.alert({
                                            msg: err.msg
                                        });
                                    }
                                });
                    } else {
                        alert('上传失败');
                    }
                });
            } else {}
        });
    });
}

你可能感兴趣的:(多张图片上传)