成人午夜视频全免费观看高清-秋霞福利视频一区二区三区-国产精品久久久久电影小说-亚洲不卡区三一区三区一区

使用微信小程序做出圖片上傳的方法-創(chuàng)新互聯(lián)

小編給大家分享一下使用微信小程序做出圖片上傳的方法,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!

公司主營業(yè)務(wù):成都做網(wǎng)站、成都網(wǎng)站建設(shè)、移動網(wǎng)站開發(fā)等業(yè)務(wù)。幫助企業(yè)客戶真正實(shí)現(xiàn)互聯(lián)網(wǎng)宣傳,提高企業(yè)的競爭能力。成都創(chuàng)新互聯(lián)公司是一支青春激揚(yáng)、勤奮敬業(yè)、活力青春激揚(yáng)、勤奮敬業(yè)、活力澎湃、和諧高效的團(tuán)隊(duì)。公司秉承以“開放、自由、嚴(yán)謹(jǐn)、自律”為核心的企業(yè)文化,感謝他們對我們的高要求,感謝他們從不同領(lǐng)域給我們帶來的挑戰(zhàn),讓我們激情的團(tuán)隊(duì)有機(jī)會用頭腦與智慧不斷的給客戶帶來驚喜。成都創(chuàng)新互聯(lián)公司推出忻府免費(fèi)做網(wǎng)站回饋大家。

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

使用微信小程序做出圖片上傳的方法

來看一下頁面效果

使用微信小程序做出圖片上傳的方法

使用微信小程序做出圖片上傳的方法

使用微信小程序做出圖片上傳的方法

查看大圖

使用微信小程序做出圖片上傳的方法

wxml文件代碼:

<view class="weui-cell"> 
    <view class="weui-cellbd"> 
     <view class="weui-uploader"> 
      <view class="weui-uploaderhd"> 
       <view class="weui-uploadertitle">營業(yè)執(zhí)照</view> 
       <view class="weui-uploaderinfo">{{imageList.length}}/{{count[countIndex]}}</view> 
      </view> 
      <view class="weui-uploaderbd"> 
       <view class="weui-uploaderfiles"> 
        <block wx:for="{{imageList}}" wx:for-item="image"> 
         <view class="weui-uploaderfile"> 
          <image class="weui-uploaderimg" src="{{image}}" src="{{image}}" bindtap="previewImage"></image> 
         </view> 
        </block> 
       </view> 
       <view class="weui-uploaderinput-box"> 
        <view class="weui-uploaderinput" 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] 
    //向服務(wù)器端上傳圖片 
    // getApp().data.servsers,這是在app.js文件里定義的后端服務(wù)器地址 
    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 后端代碼:

//獲取當(dāng)前日期時間的string類型用于文件名防重復(fù) 
  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("進(jìn)入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()); 
  //根據(jù)獲取到的文件類型截取出圖片后綴 
    String type=houzhu.substring((one+1),houzhu.length()); 
    System.out.println(multipartFile.getContentType()); 
 
    String filename; 
  // request.getRealPath獲取我們項(xiàng)目的根地址在加上我們要保存的地址 
    String realPath = request.getRealPath("/upload/wximg/"); 
    try { 
      File dir = new File(realPath); 
      if (!dir.exists()) { 
        dir.mkdir(); 
      } 
      //獲取到當(dāng)前的日期時間用戶生成文件名防止文件名重復(fù) 
      String filedata=this.dates(); 
    //生成一個隨機(jī)數(shù)來防止文件名重復(fù) 
      int x=(int)(Math.random()*1000); 
      filename="zhongshang"+x+filedata; 
      System.out.println(x); 
    將文件的地址和生成的文件名拼在一起 
      File file = new File(realPath,filename+"."+type); 
      multipartFile.transferTo(file); 
    //將圖片在項(xiàng)目中的地址和isok狀態(tài)儲存為json格式返回給前臺,由于公司項(xiàng)目中沒有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輸出的內(nèi)容:

使用微信小程序做出圖片上傳的方法

打開瀏覽器用我們的服務(wù)器的地址加上后臺返回json的dizhi字段去訪問這張圖片

使用微信小程序做出圖片上傳的方法

我們可以看到圖片已經(jīng)填入我們的服務(wù)器端里了,然后在打開我們服務(wù)器端項(xiàng)目根地址下面的/upload/wximg

使用微信小程序做出圖片上傳的方法

以上是“使用微信小程序做出圖片上傳的方法”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學(xué)習(xí)更多知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!

網(wǎng)站欄目:使用微信小程序做出圖片上傳的方法-創(chuàng)新互聯(lián)
文章URL:http://jinyejixie.com/article6/digpig.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站設(shè)計(jì)、電子商務(wù)網(wǎng)站設(shè)計(jì)公司、響應(yīng)式網(wǎng)站、品牌網(wǎng)站建設(shè)、做網(wǎng)站

廣告

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

成都定制網(wǎng)站建設(shè)
西吉县| 抚顺县| 新龙县| 阿拉尔市| 湛江市| 太保市| 隆德县| 肃北| 仁布县| 英山县| 襄汾县| 长宁县| 通化市| 扶风县| 舒兰市| 平度市| 慈溪市| 博罗县| 罗甸县| 时尚| 霍州市| 临安市| 乳山市| 嵩明县| 恭城| 金湖县| 宜黄县| 鄱阳县| 武邑县| 井陉县| 彭阳县| 蒙自县| 稻城县| 句容市| 迁安市| 连平县| 青田县| 景洪市| 通许县| 柞水县| 垦利县|