上傳檔案或者圖片到伺服器端

來源:互聯網
上載者:User

標籤:ram   document   tco   ntb   pos   handle   efi   識別   load   

html部分:

<input type="file" accept="image/png,image/jpeg"   ng-model="img"  id="img" />

js部分:

 1 $scope.postFile = function(data, url) { 2         var deferred = $q.defer(); 3         $http({ 4                 url: url, 5                 data: data, 6                 method: "post", 7                 transformRequest : angular.identity, 8                 headers: { 9                      ‘Content-Type‘ : undefined//設定為undefined,會自動識別問formdata10                 },11                 eventHandlers: {12                     progress: function(c) {13                             console.log(‘total -> ‘ + c.total+"loaded" + c.loaded);14                             console.log(c);15                         }16                 },17                 uploadEventHandlers: {//查看進度18                     progress: function(e) {19                          if(e.lengthComputable) {20                               var percentComplete = e.loaded / e.total;21                               console.log("上傳進度"+(percentComplete*100)+"%");22                          }else {23                              console.log("error!");24                          }25                     }26                 }27             })28             .then(29                 function(response) {30                     deferred.resolve(response.data);31                 },32                 function(response) {33                     deferred.reject(response);34                 }35             );36         return deferred.promise;37     }38     //預覽圖片39     $scope.preview = function() {40         var ewm_preview = document.getElementById(‘img‘).files[0];41         if(window.FileReader) {42             var fr = new FileReader();43             fr.onload = function(evt) {44                 $scope.ewm_pre_url = evt.target.result;45                 $(‘.preview_img‘).attr(‘src‘,evt.target.result);//預覽圖片元素46             }47             fr.readAsDataURL(ewm_preview);48         }49     }50     $scope.ewm_update_param = {51         "name":"",52         "type":"",53         "file":document.getElementById(‘img‘).files[0]54     }55     var formdata = new FormData();56         for(var key in $scope.ewm_update_param) {57             formdata.append(key,$scope.ewm_update_param[key])58         }59     60     $scope.postFile(formdata,‘/api/app/qrcode/upload‘).then(function(res) {61         if(res.msg=="成功" && res.content.url) {62             $scope.ewm_url = res.content.url+"&_=" + Date.now();//加時間戳記,解決緩衝問題63         }64     })

其中input的accept等屬性請參照:http://www.haorooms.com/post/input_file_leixing 

上傳檔案或者圖片到伺服器端

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.