当前位置: 移动技术网 > IT编程>开发语言>JavaScript > 微信小程序实现上传图片功能

微信小程序实现上传图片功能

2019年06月05日  | 移动技术网IT编程  | 我要评论

微信小程序图片上传,供大家参考,具体内容如下

先来看一下微信小程序的api

来看一下页面效果

查看大图

wxml文件代码:

<view class="weui-cell"> 
    <view class="weui-cell__bd"> 
     <view class="weui-uploader"> 
      <view class="weui-uploader__hd"> 
       <view class="weui-uploader__title">营业执照</view> 
       <view class="weui-uploader__info">{{imagelist.length}}/{{count[countindex]}}</view> 
      </view> 
      <view class="weui-uploader__bd"> 
       <view class="weui-uploader__files"> 
        <block wx:for="{{imagelist}}" wx:for-item="image"> 
         <view class="weui-uploader__file"> 
          <image class="weui-uploader__img" src="{{image}}" data-src="{{image}}" bindtap="previewimage"></image> 
         </view> 
        </block> 
       </view> 
       <view class="weui-uploader__input-box"> 
        <view class="weui-uploader__input" bindtap="chooseimage"></view> 
       </view> 
      </view> 
     </view> 
  </view> 
</view> 

js文件代码

chooseimage: function () { 
  var that = this; 
  console.log('aaaaaaaaaaaaaaaaaaaa') 
  
  wx.chooseimage({ 
   count: this.data.count[this.data.countindex], 
   success: function (res) { 
    console.log('ssssssssssssssssssssssssss') 
    //缓存下 
    wx.showtoast({ 
     title: '正在上传...', 
     icon: 'loading', 
     mask: true, 
     duration: 2000, 
     success: function (ress) { 
      console.log('成功加载动画'); 
     } 
    }) 
 
    console.log(res) 
    that.setdata({ 
     imagelist: res.tempfilepaths 
    }) 
    //获取第一张图片地址 
    var filep = res.tempfilepaths[0] 
    //向服务器端上传图片 
    // getapp().data.servsers,这是在app.js文件里定义的后端服务器地址 
    wx.uploadfile({ 
     url: getapp().data.servsers + '/weixin/wx_upload.do', 
     filepath: filep, 
     name: 'file', 
     formdata: { 
      'user': 'test' 
     }, 
     success: function (res) { 
      console.log(res) 
      console.log(res.data) 
      var sss= json.parse(res.data) 
      var dizhi = sss.dizhi; 
      //输出图片地址 
      console.log(dizhi); 
      that.setdata({ 
       "dizhi": dizhi 
      }) 
 
      //do something  
     }, fail: function (err) { 
      console.log(err) 
     }  
      }); 
   } 
  }) 
 }, 
 previewimage: function (e) { 
  var current = e.target.dataset.src 
 
  wx.previewimage({ 
 
   current: current, 
   urls: this.data.imagelist 
  }) 
 } 

java 后端代码:

//获取当前日期时间的string类型用于文件名防重复 
  public string dates(){ 
     date currenttime = new date(); 
     simpledateformat formatter = new simpledateformat("yyyymmddhhmmss"); 
     string datestring = formatter.format(currenttime); 
     return datestring; 
  } 
  @requestmapping("wx_upload.do") 
  public void uploadpicture(httpservletrequest request, httpservletresponse response,printwriter writer) throws exception { 
    system.out.println("进入get方法!"); 
  //获取从前台传过来得图片 
    multiparthttpservletrequest req =(multiparthttpservletrequest)request; 
    multipartfile multipartfile = req.getfile("file"); 
  //获取图片的文件类型 
    string houzhu=multipartfile.getcontenttype(); 
    int one = houzhu.lastindexof("/"); 
    system.out.println(houzhu.substring((one+1),houzhu.length())); 
    system.out.println(multipartfile.getname()); 
  //根据获取到的文件类型截取出图片后缀 
    string type=houzhu.substring((one+1),houzhu.length()); 
    system.out.println(multipartfile.getcontenttype()); 
 
    string filename; 
  // request.getrealpath获取我们项目的根地址在加上我们要保存的地址 
    string realpath = request.getrealpath("/upload/wximg/"); 
    try { 
      file dir = new file(realpath); 
      if (!dir.exists()) { 
        dir.mkdir(); 
      } 
      //获取到当前的日期时间用户生成文件名防止文件名重复 
      string filedata=this.dates(); 
    //生成一个随机数来防止文件名重复 
      int x=(int)(math.random()*1000); 
      filename="zhongshang"+x+filedata; 
      system.out.println(x); 
    将文件的地址和生成的文件名拼在一起 
      file file = new file(realpath,filename+"."+type); 
      multipartfile.transferto(file); 
    //将图片在项目中的地址和isok状态储存为json格式返回给前台,由于公司项目中没有fastjson只能用这个 
      jsonobject jsonobject=new jsonobject(); 
      jsonobject.put("isok",1); 
      jsonobject.put("dizhi","/upload/wximg/"+filename+"."+type); 
 
      writer.write(jsonobject.tostring()); 
    } catch (ioexception e) { 
      e.printstacktrace(); 
    } catch (illegalstateexception e) { 
      e.printstacktrace(); 
    } 
} 

来看一下之前在前端js输出的内容:

打开浏览器用我们的服务器的地址加上后台返回json的dizhi字段去访问这张图片

我们可以看到图片已经填入我们的服务器端里了,然后在打开我们服务器端项目根地址下面的/upload/wximg

到这里就大功告成了如果是多张图片上传可以在js里面根据要上传的数量循环上传。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网