js 选择文件部分
//选择上传文件方式
chooseFile: function () {
var that = this;
wx.showActionSheet({
itemList: [‘选择文件’, ‘拍照或相册’],
success: function (res) {
if (!res.cancel) {
if (res.tapIndex==0){
// 选择对话再选择文件
wx.chooseMessageFile({
count: 10, //能选择文件的数量
type: ‘file’, //能选择文件的类型,我这里只允许上传文件.还有视频,图片,或者都可以
success(res) {
if (res.tempFiles.length > 0) {
//循环比较
for (var i = 0; i < res.tempFiles.length; i++) {
var newupfilelist = that.data.allpath.concat(res.tempFiles[i].path) + ‘,’;
var newfilename = that.data.allfilename.concat(res.tempFiles[i].name) + ‘,’ + ‘\n’;
that.setData({
allpath: newupfilelist, //将文件的路径保存在页面的变量上,方便 wx.uploadFile调用
allfilename: newfilename //渲染到wxml方便用户知道自己选择了什么文件
})
}
}
}
})
}else{
wx.chooseImage({
count: 9,
sizeType: [‘original’, ‘compressed’],
sourceType: [‘album’, ‘camera’],
success(res) {
// tempFilePath可以作为img标签的src属性显示图片
const tempFilePaths = res.tempFilePaths
if (tempFilePaths.length > 0) {
//循环比较
for (var i = 0; i < tempFilePaths.length; i++) {
var imgname = tempFilePaths[i].split("")[1].substring(tempFilePaths[i].split("")[1].length - 20, tempFilePaths[i].split("_")[1].length);
var newupfilelist = that.data.allpath.concat(tempFilePaths[i]) + ‘,’;
var newfilename = that.data.allfilename.concat(imgname) + ‘,’ + ‘\n’;
that.setData({
allpath: newupfilelist, //将文件的路径保存在页面的变量上,方便 wx.uploadFile调用
allfilename: newfilename //渲染到wxml方便用户知道自己选择了什么文件
})
}
}
}
})
}
}
}
});
}
循环上传部分
var allpaths = that.data.allpath.split(",");
var allfilenames = that.data.allfilename.split(",");
wx.showToast({
title: "请等待,附件上传中...",
icon: "none"
})
//将图片路径循环赋值给filePath参数
for (var i = 0; i < allpaths.length-1; i++) {
var imgUrl = allpaths[i];
var filename = allfilenames[i];
wx.uploadFile({
//上传图片的网路请求地址
url: wx.getStorageSync('serverurl') + '/advisoryRecord/xcxAddUploadFile',
//选择
filePath: imgUrl,
name: 'file',
formData: {
'ordId': orderinfId,
'filename': filename,
'userid': wx.getStorageSync('openId'),
'unionid': wx.getStorageSync('unionId')
},
success: function (res) {
wx.showToast({
title: "发布成功",
icon: "none"
})
// 加跳转
wx.navigateBack({
delta: 1,
success: function (e) {
var page = getCurrentPages().pop();
if (page == undefined || page == null) return;
page.onLoad();
}
})
},
fail: function (res) {
wx.showToast({
title: "文件上传失败",
icon: "none"
})
}
});
}//for循环结束
后台Java部分
/**
* 循环上传图片文件 发布咨询的时候
*
* @param orderInf
* @return
* @param orderInf
* @return
* @param userid
* state usertype comname comcode insurancedetail expertrate insuranceuser expertcompany title tags desc accessories username imagecontents accuratePrice conditions
* @return jsonobject
*/
@RequestMapping(value = "/xcxAddUploadFile", method = RequestMethod.POST)
@ResponseBody
public JSONObject xcxAddUploadFile(HttpServletRequest request) {
JSONObject jsondata = new JSONObject();
try {
MultipartHttpServletRequest req = (MultipartHttpServletRequest) request;
// 对应前端的upload的name参数"file"
MultipartFile multipartFile = req.getFile("file");
String ordId = req.getParameter("ordId");
String userid = req.getParameter("userid");
String unionid = req.getParameter("unionid");
String filename = req.getParameter("filename");
System.out.println(ordId);
// 服务器项目路径
String pathRoot = request.getSession().getServletContext().getRealPath("");
String realPath = pathRoot.substring(0, pathRoot.lastIndexOf("\\")) + "\\insurancepic\\";
// 取得图片的格式后缀
String originalLastName = multipartFile.getOriginalFilename();
String picLastName = originalLastName.substring(originalLastName.lastIndexOf("."));
// 拼接:名字+时间戳+后缀
String picName = UUID.generate() + picLastName;
String accessories = "/insurancepic/" + picName;
System.out.println("accessories:" + accessories);
try {
File dir = new File(realPath);
// 如果文件目录不存在,创建文件目录
if (!dir.exists()) {
dir.mkdir();
System.out.println("创建文件目录成功:" + realPath);
}
File file = new File(realPath, picName);
multipartFile.transferTo(file);
if (!StringUtils.isEmpty(ordId)) {
AdvisoryRecord advisoryRecord = new AdvisoryRecord();
advisoryRecord.setId(UUID.generate());
advisoryRecord.setUserid(userid);
advisoryRecord.setUnionid(unionid);
advisoryRecord.setOrdId(ordId);
advisoryRecord.setAccessories(accessories);
advisoryRecord.setType(ConstantUtils.RECORD_TYPE_ZERO);
if (filename == null || "".equals(filename)) {
advisoryRecord.setContents(originalLastName);
} else {
advisoryRecord.setContents(filename);
}
// 增加新纪录
Boolean result = advisoryRecordDAO.addAdvisoryRecord(advisoryRecord);
// 如果增加失败
if (result == false) {
jsondata.put(ConstantUtils.STATUS, ConstantUtils.ZERO);
jsondata.put(ConstantUtils.MSG, "提交失败");
log.error(advisoryRecord.getOrdId() + "提交失败");
return jsondata;
}
}
jsondata.put(ConstantUtils.STATUS, ConstantUtils.ONE);
jsondata.put(ConstantUtils.MSG, ConstantUtils.SUCCESS);
} catch (IOException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
}
jsondata.put(ConstantUtils.STATUS, ConstantUtils.ONE);
} catch (Exception e) {
jsondata.put(ConstantUtils.STATUS, ConstantUtils.ZERO);
jsondata.put(ConstantUtils.MSG, "上传文件失败");
log.error("文件上传失败", e);
return jsondata;
}
return jsondata;
}