1. 程式人生 > >angular ng-file-upload檔案上傳

angular ng-file-upload檔案上傳

1.所需要的js檔案

<script src="angular(.min).js"></script>
<script src="ng-file-upload-shim(.min).js"></script> <!-- for no html5 browsers support -->
<script src="ng-file-upload(.min).js"></script>

2.程式碼如下

var app = angular.module('fileUpload', ['ngFileUpload']);
//Upload
app.controller('MyCtrl', ['$scope', 'Upload', function ($scope, Upload) {
        // 上傳方法 uploadFiles 
        $scope.uploadFiles = function(file, errFiles) {

            var  url = '/product/pictures/updataPicture.do'; //介面名

            $scope.f = file;
            $scope.errFile = errFiles && errFiles[0];
            if (file) {
                file.upload = Upload.upload({
                    url: url,
                    data: {uploadFileName: file, 'prodId': $scope.prodId, 'prictureGroup': $scope.prictureGroup ,'sortOrder' : $scope.sortOrder}//引數對應
                });

                file.upload.then(function (res) {
                    // 上傳成功時操作
                }, function (response) {
                    // if (response.status > 0)
                    //     $scope.errorMsg = response.status + ': ' + response.data;
                }, function (evt) {
                    // file.progress = Math.min(100, parseInt(100.0 * evt.loaded / evt.total));
                });
            }
        };  
  }]);