小程序图片上传

最近在做小程序,需要上传图片

实现的功能是:

chooseImage: function (e) {
    var that = this;
    wx.chooseImage({
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图,默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机,默认二者都有
      success: function (res) {
        // 返回选定照片的本地文件路径列表,tempFilePath可以作为img标签的src属性显示图片
        that.setData({
          files: that.data.files.concat(res.tempFilePaths)
        });
        Files = res.tempFilePaths;
        for (var i = 0; i < Files.length; i++) {
          wx.uploadFile({
            url: call.config.baseurl + 'upload/upload',//服务器地址+路径
            filePath: Files[i],
            name: 'file',
            success: function (res) {
              that.setData({
                "formdata.activityThumbnail": return_path.join(",")  //多张图片用join函数分开
              })
              return_path.push(res.data);

            }

          })

        }
      }
    });
  },
  previewImage: function (e) {
    wx.previewImage({
      current: e.currentTarget.id, // 当前显示图片的http链接
      urls: this.data.files // 需要预览的图片http链接列表
    })
  },

这里我做了一点就是一选图片就上传图片,后台得分方法没有在网上找到如何批量上穿,所以我在后台写了for循环

package com.zlf.modules.xcx.controller;

import com.zlf.core.security.shiro.authz.annotation.RequiresPathPermission;
import com.zlf.modules.activitypromotion.service.IActivityPromotionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**   
 * @Title: activity_promotion
 * @Description: activity_promotion
 * @author zy
 * @date 2018-08-25 15:59:29
 * @version V1.0   
 *
 */
@Controller
@RequestMapping("${admin.url.prefix}/xcx/upload")
@RequiresPathPermission("xcx:upload")
public class UploadController{

    @Autowired
    protected IActivityPromotionService activityPromotionService;
    /**
     * @createtime 2017年8月20日17:15:41
     * @param request
     * @param file
     * @return 上传成功返回“success”,上传失败返回“error”
     * @throws IOException
     */
    @ResponseBody
    @RequestMapping("upload")
    public String upload(HttpServletRequest request, @RequestParam(value = "file", required = false) MultipartFile file) throws IOException {
        System.out.println("执行upload");
        String savepath="";
        request.setCharacterEncoding("UTF-8");
        String user = request.getParameter("user");
        if(!file.isEmpty()) {
            String fileName = file.getOriginalFilename();
            String path = null;
            String type = null;
            type = fileName.indexOf(".") != -1 ? fileName.substring(fileName.lastIndexOf(".") + 1, fileName.length()) : null;
            if (type != null) {
                if ("GIF".equals(type.toUpperCase())||"PNG".equals(type.toUpperCase())||"JPG".equals(type.toUpperCase())) {
                    // 项目在容器中实际发布运行的根路径
                    String realPath = request.getSession().getServletContext().getRealPath("/");
                    // 自定义的文件名称
                    String trueFileName = String.valueOf(System.currentTimeMillis()) + fileName;

                    // 设置存放图片文件的路径
                    path = realPath + "upload/" + trueFileName;
                    savepath="upload/" + trueFileName;
                    System.out.println(realPath+"=================="+path);
                    file.transferTo(new File(path));
                }else {
                    return savepath;
                }
            }else {
                return savepath;
            }
        }else {
            return savepath;
        }
        return savepath;
    }

}

这样就可以实现图片多张上传

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值