效果圖:

目錄結構:

單獨定義文件上傳方法,在你需要的地方引入就可以了
httputil.js
function send_photo(data, successfun) { var that = this for (let i = 0; i < data.length; i++) { console.log("data長度=" + data.length) console.log(data) wx.uploadFile({ url: BASE_Server + "/FileUpLoadController/upload.action", //僅為示例,非真實的接口地址 自己寫映射你Java接口的路徑 filePath: data[i] + "", name: 'file', success: function(res) { successfun(res) } }) } } module.exports = { send_photo: send_photo }
引用:
.js
1 var app = getApp(); 2 const httputil = require("../../base/js/httputil.js") //一定要引入,根據你自己寫的上傳文件路徑 3 4 Page({ 5 data: { 6 photos: "/images/photo.png", 7 photos2: "/images/photo.png", 8 photos3: "/images/photo.png" 9 }, 10 formSubmit: function(e) { 11 var photo = [this.data.photos, this.data.photos2, this.data.photos3] 12 httputil.send_photo(photo, function(res) { 13 //成功回調函數 你隨便做操作 17 } 18 }, 19 touchphoto: function(e) { 20 var that = this 21 var no = e.currentTarget.id; 22 if (no == "1") { 23 wx.chooseImage({ 24 count: 1, // 默認9 25 sizeType: ['original', 'compressed'], // 可以指定是原圖還是壓縮圖,默認二者都有 26 sourceType: ['album', 'camera'], // 可以指定來源是相冊還是相機,默認二者都有 27 success: function(res) { 28 // 返回選定照片的本地文件路徑列表,tempFilePath可以作為img標簽的src屬性顯示圖片 29 var tempFilePaths = res.tempFilePaths 30 that.setData({ 31 photos: tempFilePaths 32 }) 33 console.log(that.data.photos) 34 } 35 }) 36 } else if (no == "2") { 37 wx.chooseImage({ 38 count: 1, // 默認9 39 sizeType: ['original', 'compressed'], // 可以指定是原圖還是壓縮圖,默認二者都有 40 sourceType: ['album', 'camera'], // 可以指定來源是相冊還是相機,默認二者都有 41 success: function(res) { 42 // 返回選定照片的本地文件路徑列表,tempFilePath可以作為img標簽的src屬性顯示圖片 43 var tempFilePaths = res.tempFilePaths 44 that.setData({ 45 photos2: tempFilePaths 46 }) 47 console.log(that.data.photos) 48 } 49 }) 50 } else if (no == "3") { 51 wx.chooseImage({ 52 count: 1, // 默認9 53 sizeType: ['original', 'compressed'], // 可以指定是原圖還是壓縮圖,默認二者都有 54 sourceType: ['album', 'camera'], // 可以指定來源是相冊還是相機,默認二者都有 55 success: function(res) { 56 // 返回選定照片的本地文件路徑列表,tempFilePath可以作為img標簽的src屬性顯示圖片 57 var tempFilePaths = res.tempFilePaths 58 that.setData({ 59 photos3: tempFilePaths 60 }) 61 console.log(that.data.photos) 62 } 63 }) 64 } 65 } 66 })
wxml
<text class='textp'>身份證照片</text> <text class='textpp'>駕駛證照片</text> <view class='photo'> <image class='ph' src='{{photos}}'id="1" bindtap='touchphoto' mode='aspectFit'></image> <image class='ph' src='{{photos2}}' id="2" bindtap='touchphoto' mode='aspectFit'></image> <image class='phright' src='{{photos3}}' id="3" bindtap='touchphoto' mode='aspectFit'></image> </view> <text class='text'>正面</text><text class='text2'>反面</text><text class='text3'>人像面</text>
wxss
.photo { display: flex; margin-top: 10px; height: 100px; } .ph { border: 1px dashed #909090; margin-right: 30px; width: 80px; height: 60px; } .phzz { border: 1px dashed #909090; margin-right: 70px; margin-left: 70px; width: 100px; height: 60px; } .phright{ border: 1px dashed #909090; margin-left: 20px; width: 80px; height: 60px; } .textp{ margin-left: 70px; font-size: 14px; } .text{ margin-left: 25px; font-size: 14px; } .text2{ margin-left: 80px; font-size: 14px; } .text3{ margin-left: 98px; font-size: 14px; }
java代碼:
import com.alibaba.fastjson.JSON; import org.apache.commons.fileupload.FileItem; import org.apache.commons.fileupload.FileUploadException; import org.apache.commons.fileupload.disk.DiskFileItemFactory; import org.apache.commons.fileupload.servlet.ServletFileUpload; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.multipart.MultipartFile; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.*; import java.util.HashMap; import java.util.List; /** * Created by majl on 2017/9/1. */ @Controller @RequestMapping("/upload") public class UploadController { private static final Logger logger = LoggerFactory.getLogger(UploadController.class); @RequestMapping("/picture") public void uploadPicture(HttpServletRequest request, HttpServletResponse response) throws Exception { //獲取文件需要上傳到的路徑 String path = request.getRealPath("/upload") + "/"; File dir = new File(path); if (!dir.exists()) { dir.mkdir(); } logger.debug("path=" + path); request.setCharacterEncoding("utf-8"); //設置編碼 //獲得磁盤文件條目工廠 DiskFileItemFactory factory = new DiskFileItemFactory(); //如果沒以下兩行設置的話,上傳大的文件會占用很多內存, //設置暫時存放的存儲室,這個存儲室可以和最終存儲文件的目錄不同 /** * 原理: 它是先存到暫時存儲室,然后再真正寫到對應目錄的硬盤上, * 按理來說當上傳一個文件時,其實是上傳了兩份,第一個是以 .tem 格式的 * 然后再將其真正寫到對應目錄的硬盤上 */ factory.setRepository(dir); //設置緩存的大小,當上傳文件的容量超過該緩存時,直接放到暫時存儲室 factory.setSizeThreshold(1024 * 1024); //高水平的API文件上傳處理 ServletFileUpload upload = new ServletFileUpload(factory); try { List<FileItem> list = upload.parseRequest(request); FileItem picture = null; for (FileItem item : list) { //獲取表單的屬性名字 String name = item.getFieldName(); //如果獲取的表單信息是普通的 文本 信息 if (item.isFormField()) { //獲取用戶具體輸入的字符串 String value = item.getString(); request.setAttribute(name, value); logger.debug("name=" + name + ",value=" + value); } else { picture = item; } } //自定義上傳圖片的名字為userId.jpg String fileName = request.getAttribute("userId") + ".jpg"; String destPath = path + fileName; logger.debug("destPath=" + destPath); //真正寫到磁盤上 File file = new File(destPath); OutputStream out = new FileOutputStream(file); InputStream in = picture.getInputStream(); int length = 0; byte[] buf = new byte[1024]; // in.read(buf) 每次讀到的數據存放在buf 數組中 while ((length = in.read(buf)) != -1) { //在buf數組中取出數據寫到(輸出流)磁盤上 out.write(buf, 0, length); } in.close(); out.close(); } catch (FileUploadException e1) { logger.error("", e1); } catch (Exception e) { logger.error("", e); } PrintWriter printWriter = response.getWriter(); response.setContentType("application/json"); response.setCharacterEncoding("utf-8"); HashMap<String, Object> res = new HashMap<String, Object>(); res.put("success", true); printWriter.write(JSON.toJSONString(res)); printWriter.flush(); } --------------------- 作者:庭然 來源:CSDN 這段代碼我在網上找的最簡單好用的,我本地寫的接口封裝類過多,就不寫了
