国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 課堂 > 小程序 > 正文

微信小程序上傳圖片實例

2020-03-21 16:17:21
字體:
來源:轉載
供稿:網友

在網上看了好多小程序上傳圖片,java后端接收的示例,但是不管在哪個網站看的,代碼基本是一樣的,都是代碼特別多。

所以就自己寫一個比較簡單的。

 一 小程序端

user.wxml

<view class='user_head'>  <view>   <image src='{{ptuser.avatarUrl}}' bindtap='updateHead'></image>  </view>  <text>點擊選擇頭像</text> </view>

user.js

// 更換頭像 span style="font-size:18px;color:#FF0000;"> updateHead: function () {   var that = this   // 上傳圖片 獲取路徑   wx.chooseImage({    success: function (res) {     console.log('臨時路徑:' + res.tempFilePaths[0])       wx.uploadFile({        url: app.globalData.baseUrl + '/file/uploadFile',        filePath: res.tempFilePaths[0],        name: 'file',        success: function (result) {         console.log("返回路徑:" + result.data)        }       })    },   })  },

 二 java端

package cn.helloxhs.moudle.common;  import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream;  import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;  import org.apache.commons.fileupload.disk.DiskFileItem; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.commons.CommonsMultipartFile;  import cn.helloxhs.base.controller.BaseController;  /**  * 類說明  *  * @author 肖荷山  * @version 創建時間:2017年12月23日 上午11:14:27  */ @Controller @RequestMapping("/file") public class FileController extends BaseController {   @RequestMapping("/uploadFile")   @ResponseBody   public Object uploadFile(HttpServletResponse response, HttpServletRequest request, MultipartFile file) {     String realPath = request.getSession().getServletContext().getRealPath("/temp");     try {       CommonsMultipartFile cf = (CommonsMultipartFile) file;       DiskFileItem fi = (DiskFileItem) cf.getFileItem();       File f1 = fi.getStoreLocation();       InputStream ips = new FileInputStream(f1);       OutputStream ops = new FileOutputStream(realPath + "/" + "xhs.jpg");       byte[] b = new byte[1024];       int len;       try {         while ((len = ips.read(b)) != -1) {           ops.write(b, 0, len);         }       } catch (IOException e) {         e.printStackTrace();       } finally {         // 完畢,關閉所有鏈接         try {           ops.close();           ips.close();         } catch (IOException e) {           e.printStackTrace();         }       }      } catch (FileNotFoundException e) {       e.printStackTrace();     }     return realPath;   }  } 

圖片存在了項目的temp目錄下

微信小程序,上傳圖片

簡單就好,沒其他功能,單純上傳圖片。

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VEVB武林網。


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 虹口区| 女性| 安多县| 泌阳县| 汤原县| 英德市| 门源| 且末县| 乌拉特中旗| 吉林市| 合山市| 彭阳县| 班戈县| 婺源县| 金湖县| 北川| 堆龙德庆县| 河曲县| 马尔康县| 平陆县| 日土县| 即墨市| 合江县| 收藏| 呈贡县| 五台县| 永嘉县| 东丰县| 格尔木市| 香格里拉县| 和龙市| 左权县| 衡阳县| 嵊州市| 望都县| 青铜峡市| 东乌珠穆沁旗| 宽城| 诸城市| 永顺县| 乐陵市|