搜尋
首頁微信小程式小程式開發如何使用微信小程式做出圖片上傳

這次帶給大家如何使用微信小程式做出圖片上傳,使用微信小程式做出圖片上傳的注意事項有哪些,以下就是實戰案例,一起來看一下。

先來看微信小程式的api

來看看頁面效果

#看大圖

#wxml檔案程式碼:

<view> 
    <view> 
     <view> 
      <view> 
       <view>营业执照</view> 
       <view>{{imageList.length}}/{{count[countIndex]}}</view> 
      </view> 
      <view> 
       <view> 
        <block> 
         <view> 
          <image></image> 
         </view> 
        </block> 
       </view> 
       <view> 
        <view></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裡面根據要上傳的數量循環上傳。

相信看了本文案例你已經掌握了方法,更多精彩請關注php中文網其它相關文章!

推薦閱讀:

關於js的三種使用方式案例詳解(附程式碼)

##JS載入方式使用匯總

以上是如何使用微信小程式做出圖片上傳的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

將Eclipse與SAP NetWeaver應用伺服器整合。

DVWA

DVWA

Damn Vulnerable Web App (DVWA) 是一個PHP/MySQL的Web應用程序,非常容易受到攻擊。它的主要目標是成為安全專業人員在合法環境中測試自己的技能和工具的輔助工具,幫助Web開發人員更好地理解保護網路應用程式的過程,並幫助教師/學生在課堂環境中教授/學習Web應用程式安全性。 DVWA的目標是透過簡單直接的介面練習一些最常見的Web漏洞,難度各不相同。請注意,該軟體中

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

VSCode Windows 64位元 下載

VSCode Windows 64位元 下載

微軟推出的免費、功能強大的一款IDE編輯器