1. 程式人生 > >Thinkphp3.2+ 微信小程式圖片上傳

Thinkphp3.2+ 微信小程式圖片上傳

wx.uploadFile(OBJECT)

將本地資源上傳到開發者伺服器,客戶端發起一個 HTTPS POST 請求,其中 content-type 為 multipart/form-data 。使用前請先閱讀說明

如頁面通過 wx.chooseImage 等介面獲取到一個本地資源的臨時檔案路徑後,可通過此介面將本地資源上傳到指定伺服器。

官方小程式例項程式碼

示例程式碼:

wx.chooseImage({
  success: function(res) {
    var tempFilePaths = res.tempFilePaths
    wx.uploadFile({
      url: 'https://example.weixin.qq.com/upload'
, //僅為示例,非真實的介面地址 filePath: tempFilePaths[0], name: 'file', formData:{ 'user': 'test' }, success: function(res){ var data = res.data //do something } }) } })
php後臺程式碼    public function uploadImage(){
        $id = I('get.id');
        $url = $this->upload();//獲取到圖片 
        $where = array('id'=>$id);

        $res = M('onlintable')->where($where)->find();

           //需要判斷一下該條資料上面是否存在圖片

        if($res['url']==''||$res['url']==null){
            $str = $url;

        }else{

            拼接與喜愛圖片地址

            $str = $res['url'].','.$url;
        }

        $save = array('url'=>$str);

            //儲存下來

        $data = M('onlintable')->where($where)->save($save);
        if($data){
            echo true;
        }else{
            echo false;
        }

    }

upload還是之前的單檔案上傳