国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

微信小程序圖片選擇、上傳到服務器、預覽(PHP)實現實例

2019-11-19 16:36:27
字體:
來源:轉載
供稿:網友

微信小程序圖片選擇、上傳到服務器、預覽(PHP)實現實例

小程序實現選擇圖片、預覽圖片、上傳到開發者服務器上

后臺使用的tp3.2 圖片上傳

請求時候的header參考時可以去掉(個人后臺驗證權限使用)

小程序前端代碼:

<view class="section"> <form bindsubmit="bindFormSubmit">  <textarea placeholder="請輸入問題內容" name="content"/>  <view class="">   選擇提問圖片:  <label bindtap="checkimg">點擊選擇圖片</label>  </view>  <view class="">    <image wx:for="{{imglist}}" mode="aspectFit" bindtap="ylimg" data-src="{{item}}" style="width:75px;height:75px;" src="{{item}}"></image>  </view>  <button form-type="submit"> 提交 </button> </form></view>

小程序js代碼:

data: {  imglist:[] },/**  * form提交事件  */ bindFormSubmit:function(e){   self=this   //圖片   var imglist = self.data.imglist   //提問內容   var content=e.detail.value.content;   if(content==''){    wx.showToast({     title: '內容不能為空',     icon: 'loading',     duration: 1000,     mask:true    })   }   wx.showLoading({    title: '正在提交...',    mask:true   })   //添加問題   wx.request({    url: 'https://xxxxxxxxxx/index.PHP?g=user&m=center&a=createwt',    data: {     content:content    },    method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT    header: app.globalData.header, // 設置請求的 header    success: function (res) {     // success     if(typeof(res.data)=='number'){      if (imglist != '') {       //開始插入圖片       for(var i=0;i<imglist.length;i++){        //上傳至服務器        wx.uploadFile({         url: 'https://xxxxxxxx/index.php?g=user&m=center&a=upload', //僅為示例,非真實的接口地址         filePath: imglist[0],         name: 'files',         formData: {          'wtid': res.data         },         header: app.globalData.header,         success: function (res) {          if(i>=imglist.length){           self.setData({            imglist:[]           })           wx.hideLoading();           wx.showToast({            title: '提問成功',            icon: 'success',            duration: 2000,            mask: true           })           wx.navigateBack({            delta: 1           })          }         }        })       }       console.log(imglist);      }else{       wx.hideLoading();       wx.showToast({        title: '提問成功',        icon: 'success',        duration: 2000,        mask: true       })       wx.navigateBack({        delta: 1       })      }     }else{      wx.hideLoading();      wx.showToast({       title: res.data,       icon: 'loading',       duration: 1000,       mask: true      })     }    },    fail: function (res) {     self.onLoad();    }   }) }, //點擊選擇圖片 checkimg:function(){   console.log('點擊選擇圖片');   self=this   wx.chooseImage({    count: 9, // 默認9    sizeType: ['original', 'compressed'], // 可以指定是原圖還是壓縮圖,默認二者都有    sourceType: ['album', 'camera'], // 可以指定來源是相冊還是相機,默認二者都有    success: function (res) {     // 返回選定照片的本地文件路徑列表,tempFilePath可以作為img標簽的src屬性顯示圖片     var tempFilePaths = res.tempFilePaths     self.setData({      imglist:tempFilePaths     })    }   }) }, //點擊預覽圖片 ylimg:function(e){  wx.previewImage({   current: e.target.dataset.src,   urls: this.data.imglist // 需要預覽的圖片http鏈接列表  }) }

php后臺代碼

//圖片上傳

public function upload(){if(IS_POST){$upload = new /Think/Upload();// 實例化上傳類$upload->maxSize  =   3145728 ;// 設置附件上傳大小$upload->exts   =   array('jpg', 'gif', 'png', 'jpeg');// 設置附件上傳類型$upload->rootPath =   './Uploads/'; // 設置附件上傳根目錄$upload->savePath =   ''; // 設置附件上傳(子)目錄// 上傳文件 $info  =  $upload->upload();if(!$info) {// 上傳錯誤提示錯誤信息  $this->error($upload->getError());}else{// 上傳成功 獲取上傳文件信息//插入到數據庫中}}}

感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 阜平县| 富裕县| 宁安市| 十堰市| 合作市| 延边| 康乐县| 田东县| 莫力| 靖江市| 汉川市| 镇原县| 黄冈市| 阿鲁科尔沁旗| 曲阜市| 台中县| 丘北县| 德令哈市| 洛南县| 望谟县| 济宁市| 抚顺县| 南昌县| 淳安县| 筠连县| 辽阳县| 武城县| 高雄市| 临海市| 望谟县| 石棉县| 甘孜县| 开平市| 阳谷县| 屯留县| 中西区| 赣榆县| 云安县| 承德县| 烟台市| 武义县|