微信小程序圖片選擇、上傳到服務器、預覽(PHP)實現實例
小程序實現選擇圖片、預覽圖片、上傳到開發者服務器上
后臺使用的tp3.2 圖片上傳
請求時候的header參考時可以去掉(個人后臺驗證權限使用)
小程序前端代碼:
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: http://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: http://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{// 上傳成功 獲取上傳文件信息//插入到數據庫中}以上就是本文的全部內容,希望對大家的學習有所幫助,更多相關內容請關注PHP !
相關推薦:
如何使用PHP導出數據到淘寶助手CSV
PHP實現微信公眾平臺企業號驗證接口
以上就是PHP實現微信小程序上圖片選擇及上傳到服務器和預覽的詳細內容,PHP教程
鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯系我們修改或刪除,多謝。
新聞熱點
疑難解答