Analysis of practical cases of uploading pictures in WeChat applet

php中世界最好的语言
Release: 2018-05-31 14:11:21
Original
2478 people have browsed it

This time I will bring you an analysis of a practical case of uploading images to WeChat mini programs. What are the precautions for practical cases of uploading images to WeChat mini programs? The following is a practical case, let’s take a look.

I have seen a lot of examples on the Internet of small programs uploading images and java back-end reception, but no matter which website I read, the code is basically the same, and there is a lot of code.

So I just wrote a simpler one myself.

One small terminal

user.wxml

<view class=&#39;user_head&#39;> 
 <view> 
  <image src=&#39;{{ptuser.avatarUrl}}&#39; bindtap=&#39;updateHead&#39;></image> 
 </view> 
 <text>点击选择头像</text> 
</view>
Copy after login

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) 
       } 
      }) 
   }, 
  }) 
 },
Copy after login

Two java terminal

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; 
  } 
 
}
Copy after login

The picture is stored in the temp directory of the project

It’s simple, it has no other functions, just upload pictures.

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!

Recommended reading:

How to use WeChat applet to upload pictures

How to operate JS to generate random numbers and Random sequence

The above is the detailed content of Analysis of practical cases of uploading pictures in WeChat applet. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template