微信小程序上傳圖片實(shí)例
在網(wǎng)上看了好多小程序上傳圖片,java后端接收的示例,但是不管在哪個(gè)網(wǎng)站看的,代碼基本是一樣的,都是代碼特別多。
所以就自己寫一個(gè)比較簡單的。
一 小程序端
user.wxml
<view class='user_head'>
<view>
<image src='{{ptuser.avatarUrl}}' bindtap='updateHead'></image>
</view>
<text>點(diǎn)擊選擇頭像</text>
</view>
user.js
// 更換頭像
span style="font-size:18px;color:#FF0000;"> updateHead: function () {
var that = this
// 上傳圖片 獲取路徑
wx.chooseImage({
success: function (res) {
console.log('臨時(shí)路徑:' + 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 創(chuàng)建時(shí)間: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 {
// 完畢,關(guān)閉所有鏈接
try {
ops.close();
ips.close();
} catch (IOException e) {
e.printStackTrace();
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return realPath;
}
}
圖片存在了項(xiàng)目的temp目錄下

簡單就好,沒其他功能,單純上傳圖片。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
JavaScript實(shí)現(xiàn)將數(shù)組數(shù)據(jù)添加到Select下拉框的方法
這篇文章主要介紹了JavaScript實(shí)現(xiàn)將數(shù)組數(shù)據(jù)添加到Select下拉框的方法,涉及javascript數(shù)組操作及頁面元素動(dòng)態(tài)賦值的相關(guān)技巧,需要的朋友可以參考下2015-08-08
基于JS實(shí)現(xiàn)簡單的隨機(jī)抽取幸運(yùn)員工系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了基于HTML+JavaScript實(shí)現(xiàn)簡單的隨機(jī)抽取幸運(yùn)員工系統(tǒng),文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起了解一下2023-11-11
js實(shí)現(xiàn)首屏延遲加載實(shí)現(xiàn)方法 js實(shí)現(xiàn)多屏單張圖片延遲加載效果
這篇文章主要介紹了js實(shí)現(xiàn)首屏延遲加載實(shí)現(xiàn)方法,以及js實(shí)現(xiàn)多屏單張圖片延遲加載效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07
解讀input標(biāo)簽的value屬性及name屬性
這篇文章主要介紹了解讀input標(biāo)簽的value屬性,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-01-01
微信小程序用canvas實(shí)現(xiàn)圓形進(jìn)度條
這篇文章主要為大家詳細(xì)介紹了微信小程序用canvas實(shí)現(xiàn)圓形進(jìn)度條,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-06-06
js getBoundingClientRect() 來獲取頁面元素的位置
該方法已經(jīng)不再是IE Only了,F(xiàn)F3.0+和Opera9.5+已經(jīng)支持了該方法,可以說在獲得頁面元素位置上效率能有很大的提高,在以前版本的Opera和Firefox中必須通過循環(huán)來獲得元素在頁面中的絕對位置。2010-11-11
JavaScript 組件之旅(四):測試 JavaScript 組件
本期,我們要討論的話題是 JavaScript 的測試,以檢查組件的狀態(tài)和工作方式是否符合預(yù)期,還會介紹一個(gè)可以方便編寫測試用例的測試方法。這里說的測試當(dāng)然是使用自動(dòng)化的測試手段,這是軟件質(zhì)量保證(QA)的重要環(huán)節(jié)。2009-10-10

