由于公司最近要通过钉钉审批连接后台系统,实现办公电子化,所以会出现这种需求
钉钉小程序提供的图片上传说明
dd.uploadFile({
url: '请使用自己服务器地址',
fileType: 'image',
fileName: 'file',
filePath: '...',
success: (res) => {
dd.alert({
content: '上传成功'
});
},
});
封装后的代码
dd.showLoading({ content: "图片上传中..." });
try {
let bindThis = this;
let promiseArr = [];
for (let i = 0; i < this.data.moneyImages.length; i++) {
let item = this.data.moneyImages[i];
if (item.src != "添加" && item.upload == "0") {
promiseArr.push(new Promise(function(resolve, reject) {
dd.uploadFile({
url: uploadUrl,
filePath: item.src,
fileName: 'test.jpeg',//随便写没有影响
fileType: 'image',
success: (res) => {
console.log(res);
let successData = JSON.parse(res.data);
if (successData.success == false) {
reject(successData);
} else {
item.upload = "1";
item.outPath = successData.result.outFilePath;
resolve();
}
},
fail: (err) => {
reject(err);
}
})
})
)
}
}
Promise.all(promiseArr).then((res) => {
dd.hideLoading();
//提交表单到审批流程
bindThis.startSubmitFormData();
}).catch((error) => {
console.log('失败')
let code = error.code;
dd.hideLoading();
if (code != undefined) {
dd.alert({
title: '提示',
content: error.message,
buttonText: '好的',
});
} else {
dd.alert({
title: '提示',
content: '图片上传失败,请点击提交重传',
buttonText: '好的',
});
}
})
} catch (e) {
dd.hideLoading();
console.log(e)
}
只是做了简单的封装,大家也可以封装成function然后方便调用,我是直接写在逻辑层了
对应的java后台代码也一并给出吧(此处是做了图片MD5校验的)
/**
* 上传图片
* @param params 请求参数
* @param request 上传图片请求
* @return
*/
@RequestMapping(value = "/dingding/uploadImages",method = RequestMethod.POST)
@ResponseBody
public ServiceResult uploadImage(@RequestParam Map<String,Object> params, HttpServletRequest request){
Map<String,Object> resultMap = new HashMap<>();
"此处对应要转换成StandardMultipartHttpServletRequest,否则没法获取multifile对象请注意"
StandardMultipartHttpServletRequest httpServletRequest = (StandardMultipartHttpServletRequest) request;
Iterator<String> iterator = httpServletRequest.getFileNames();
Boolean isHavePic = false;
while (iterator.hasNext()) {
MultipartFile file = httpServletRequest.getFile(iterator.next());
String fileNames = file.getOriginalFilename();
String imageMd5 = ImageMD5Util.getMd5(file);//图片的md5值
P2pApprovalImage singleImage = imageDao.selectApproveImgae(imageMd5);
if (singleImage!=null){
isHavePic = true;
break;
}
}
ServiceResult service = null;
if (isHavePic){
service = ServiceResult.failure("201","图片重复,请勿重复提交申请");
}else{
Iterator<String> iteratorUpload = httpServletRequest.getFileNames();
String isUploadSuccess = "";
while (iteratorUpload.hasNext()) {
MultipartFile file = httpServletRequest.getFile(iteratorUpload.next());
String fileNames = file.getOriginalFilename();
String imageMd5 = ImageMD5Util.getMd5(file);//图片的md5值
P2pApprovalImage singleImage = imageDao.selectApproveImgae(imageMd5);
bizLogger.info("存储文件==" + fileNames + "===存储路径" + filePath);
isUploadSuccess = saveFile(file, filePath,imageMd5);
if (isUploadSuccess.length()>0){
//上传失败
break;
}
}
if (isUploadSuccess.length()>0){
resultMap.put("outFilePath",isUploadSuccess);
service = ServiceResult.success(resultMap);
}else{
service = ServiceResult.failure("202","图片上传失败,请重试");
}
}
return service;
}
有问题及时沟通吧,下面有我的联系方式。