微信小程序如實(shí)現(xiàn)上傳圖片功能

這篇文章給大家分享的是有關(guān)微信小程序如實(shí)現(xiàn)上傳圖片功能的內(nèi)容。小編覺(jué)得挺實(shí)用的,因此分享給大家做個(gè)參考,一起跟隨小編過(guò)來(lái)看看吧。

成都創(chuàng)新互聯(lián)堅(jiān)持“要么做到,要么別承諾”的工作理念,服務(wù)領(lǐng)域包括:成都網(wǎng)站設(shè)計(jì)、做網(wǎng)站、成都外貿(mào)網(wǎng)站建設(shè)公司、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣等服務(wù),滿足客戶于互聯(lián)網(wǎng)時(shí)代的謝通門(mén)網(wǎng)站設(shè)計(jì)、移動(dòng)媒體設(shè)計(jì)的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎng)絡(luò)建設(shè)合作伙伴!

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

所以就自己寫(xiě)一個(gè)比較簡(jiǎn)單的。

一 小程序端

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; 
 
/** 
 * 類說(shuō)明 
 * 
 * @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目錄下

微信小程序如實(shí)現(xiàn)上傳圖片功能

簡(jiǎn)單就好,沒(méi)其他功能,單純上傳圖片。

感謝各位的閱讀!關(guān)于“微信小程序如實(shí)現(xiàn)上傳圖片功能”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,讓大家可以學(xué)到更多知識(shí),如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!

文章標(biāo)題:微信小程序如實(shí)現(xiàn)上傳圖片功能
文章路徑:http://bm7419.com/article10/pciego.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供移動(dòng)網(wǎng)站建設(shè)、網(wǎng)站制作、營(yíng)銷型網(wǎng)站建設(shè)、域名注冊(cè)定制開(kāi)發(fā)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

成都定制網(wǎng)站網(wǎng)頁(yè)設(shè)計(jì)