最近在做小程序,需要上传图片
实现的功能是:
chooseImage: function (e) {
var that = this;
wx.chooseImage({
sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图,默认二者都有
sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机,默认二者都有
success: function (res) {
// 返回选定照片的本地文件路径列表,tempFilePath可以作为img标签的src属性显示图片
that.setData({
files: that.data.files.concat(res.tempFilePaths)
});
Files = res.tempFilePaths;
for (var i = 0; i < Files.length; i++) {
wx.uploadFile({
url: call.config.baseurl + 'upload/upload',//服务器地址+路径
filePath: Files[i],
name: 'file',
success: function (res) {
that.setData({
"formdata.activityThumbnail": return_path.join(",") //多张图片用join函数分开
})
return_path.push(res.data);
}
})
}
}
});
},
previewImage: function (e) {
wx.previewImage({
current: e.currentTarget.id, // 当前显示图片的http链接
urls: this.data.files // 需要预览的图片http链接列表
})
},
这里我做了一点就是一选图片就上传图片,后台得分方法没有在网上找到如何批量上穿,所以我在后台写了for循环
package com.zlf.modules.xcx.controller;
import com.zlf.core.security.shiro.authz.annotation.RequiresPathPermission;
import com.zlf.modules.activitypromotion.service.IActivityPromotionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
/**
* @Title: activity_promotion
* @Description: activity_promotion
* @author zy
* @date 2018-08-25 15:59:29
* @version V1.0
*
*/
@Controller
@RequestMapping("${admin.url.prefix}/xcx/upload")
@RequiresPathPermission("xcx:upload")
public class UploadController{
@Autowired
protected IActivityPromotionService activityPromotionService;
/**
* @createtime 2017年8月20日17:15:41
* @param request
* @param file
* @return 上传成功返回“success”,上传失败返回“error”
* @throws IOException
*/
@ResponseBody
@RequestMapping("upload")
public String upload(HttpServletRequest request, @RequestParam(value = "file", required = false) MultipartFile file) throws IOException {
System.out.println("执行upload");
String savepath="";
request.setCharacterEncoding("UTF-8");
String user = request.getParameter("user");
if(!file.isEmpty()) {
String fileName = file.getOriginalFilename();
String path = null;
String type = null;
type = fileName.indexOf(".") != -1 ? fileName.substring(fileName.lastIndexOf(".") + 1, fileName.length()) : null;
if (type != null) {
if ("GIF".equals(type.toUpperCase())||"PNG".equals(type.toUpperCase())||"JPG".equals(type.toUpperCase())) {
// 项目在容器中实际发布运行的根路径
String realPath = request.getSession().getServletContext().getRealPath("/");
// 自定义的文件名称
String trueFileName = String.valueOf(System.currentTimeMillis()) + fileName;
// 设置存放图片文件的路径
path = realPath + "upload/" + trueFileName;
savepath="upload/" + trueFileName;
System.out.println(realPath+"=================="+path);
file.transferTo(new File(path));
}else {
return savepath;
}
}else {
return savepath;
}
}else {
return savepath;
}
return savepath;
}
}
这样就可以实现图片多张上传