小程序-js-批量上传文件-上传图片-封装-file-uploader.js-递归调用
/**
* 文件上传工具(后期可以把Img都换成File)
*
* file-uploader.js
*/
//调用示例:
//var fileUploader = require("../../../utils/file-uploader.js");
// fileUploader.doUploadImgs(_this, uploadPhotosInfos, function (uploadAllCallback) {
// console.log("上传文件框架回调结果---" + JSON.stringify(uploadAllCallback));
// // { "isAllSuccess":true, "totalCount":1, "successCount":1, "successUploadInfos":{ "http://tmp/wx50dd5a7c9d6fd93a.o6zAJszH6vI-5Mw190wEucpfXnaQ.SdsDWT5a5363b670fc97fa36b5cda77d737d38dc91de.png":"upload/a24d6996-a03f-4fa5-8086-af2f8caac5d5.png" }}
// if (uploadAllCallback.isAllSuccess == true) {
// //TODO something...
// } else {
// wx.showModal({
// title: '警告:文件上传结果',
// content: '有' + (totalCount - successCount) + "张文件上传失败,继续保存?",
// success: function (res) {
// if (res.confirm) {
// console.log('用户点击确定')
// //TODO something...
// } else if (res.cancel) {
// console.log('用户点击取消')
// }
// }
// })
// }
// }
var configapi = require("./../config/appconfig.js");
//如果后续发现不同的模块引入此appconfig模块有路径错误时,就把这个路径当成参数传入
function _doUploadImgs(_this, uploadPhotosInfos, uploadAllCallback) {
var uploadAllCallbackJSON = {
isAllSuccess: false,
totalCount: 0,
successCount: 0,
successUploadInfos: {}
};
if (!uploadPhotosInfos || uploadPhotosInfos.length == 0) {
return uploadAllCallbackJSON;
}
var _totalCount = uploadPhotosInfos.length;
uploadAllCallbackJSON.totalCount = _totalCount;
//初始化
lastUploadImgIndex = 0;
uploadAllResArr.splice(0, uploadAllResArr.length);
uploadImgsAll(uploadPhotosInfos, function (uploadAllRes) {
console.log("所有照片上传结束----共" + _totalCount + "张");
console.log("所有照片上传结束----共" + JSON.stringify(uploadAllRes));
// [{ "imgOrignPath": "http://tmp/wx50dd5a7c9d6fd93a.o6zAJszH6vI-5Mw190wEucpfXnaQ.SdsDWT5a5363b670fc97fa36b5cda77d737d38dc91de.png", "imgReletivevalUrl": "upload/f335a92b-53de-4f87-8021-cb8064747762.png" }]
var _successCount = uploadAllRes.length;
if (_successCount != _totalCount) {
console.log("所有文件上传结束--共" + _totalCount + "张,但有" + (_totalCount - _successCount) + "张上传失败");
uploadAllCallbackJSON.isAllSuccess = false;
} else {
console.log("所有文件上传结束-共" + _totalCount + "张,且全部上传成功");
uploadAllCallbackJSON.isAllSuccess = true;
}
uploadAllCallbackJSON.successCount = _successCount;
var uploadAllDetail = {};
//数组转成json形式 更方便处理 转成 key value形式
for (var i = 0; i < uploadAllRes.length; i++) {
var _item = uploadAllRes[i];
var _key = _item.imgOrignPath;
var _value = _item.imgReletivevalUrl;
uploadAllDetail[_key] = _value;
}
uploadAllCallbackJSON.successUploadInfos = uploadAllDetail;
uploadAllCallback && typeof uploadAllCallback == 'function' && uploadAllCallback(uploadAllCallbackJSON)
});
}
//定义
var uploadAllResArr = [];
var lastUploadImgIndex = 0;
function uploadImgsAll(uploadPhotosInfos, cb) {
var totalCount = uploadPhotosInfos.length;
uploadImgOne(uploadPhotosInfos, lastUploadImgIndex, function (uploadOneRes) {
++lastUploadImgIndex;
// oneRes.imgIndex = lastUploadImgIndex;
// oneRes.imgOrignPath = _imgPath;
// oneRes.imgReletivevalUrl = "";
// oneRes.errormsg = "innererr";
// oneRes.success = false;
var _s = uploadOneRes.success;
if (_s == true) {
var uploadAllRes = {}
uploadAllRes.imgOrignPath = uploadOneRes.imgOrignPath;
uploadAllRes.imgReletivevalUrl = uploadOneRes.imgReletivevalUrl;
uploadAllResArr.push(uploadAllRes);
} else {
//TODO 后期记录下失败的原始Path
}
if (lastUploadImgIndex > totalCount - 1) {//最后一张都已近传完了,完全结束
cb(uploadAllResArr);
return;
} else {
uploadImgsAll(uploadPhotosInfos, cb);
}
})
}
function uploadImgOne(uploadPhotosInfos, lastUploadImgIndex, cb) {//imgOrignPath
var _uploadFileUrl = configapi.getUploadFileHost() + "UploadHandler.ashx?action=minpupload&FileModule=projectinspect&FileType=imgaes&FileName=test-0427&FileExtName=jpg";//这些参数暂时不需要
console.log('_uploadFileUrl=' + _uploadFileUrl);
var _imgPath = uploadPhotosInfos[lastUploadImgIndex];
var oneRes = {}
if (!_imgPath) {
oneRes.imgIndex = lastUploadImgIndex;
oneRes.imgOrignPath = _imgPath;
oneRes.imgReletivevalUrl = "";
oneRes.errormsg = "undefineFilePath";
oneRes.success = false;
cb(oneRes)
return;
}
wx.uploadFile({
url: _uploadFileUrl,
filePath: _imgPath,
name: 'file',//这里根据自己的实际情况改
formData: null,
success: function (res) {
var _data = res.data
_data = JSON.parse(_data)
console.log('结果返回res.data==' + JSON.stringify(_data));
if (_data.code == 0) {
var _imgReletivevalUrl = _data.result;
oneRes.imgIndex = lastUploadImgIndex;
oneRes.imgOrignPath = _imgPath;
oneRes.imgReletivevalUrl = _imgReletivevalUrl;
oneRes.errormsg = "ok";
oneRes.success = true;
} else {
oneRes.imgIndex = lastUploadImgIndex;
oneRes.imgOrignPath = _imgPath;
oneRes.imgReletivevalUrl = "";
oneRes.errormsg = "innererr" + _data.message;;
oneRes.success = false;
}
cb(oneRes)
},
fail: function () {
oneRes.imgIndex = lastUploadImgIndex;
oneRes.imgOrignPath = _imgPath;
oneRes.imgReletivevalUrl = "";
oneRes.errormsg = "failcallback";
oneRes.success = false;
cb(oneRes)
}
})
}
module.exports={
doUploadImgs: _doUploadImgs
}
正文到此结束