javascript
  • html
  • angularjs
  • ng-file-upload
  • 2016-06-26 58 views 1 likes 
    1

    我想輸入圖像並將其上傳到本地機器上的文件夾。ng-file-upload不起作用

    HTML:

    <md-input-container class="md-block" flex-gt-sm> 
        <h4>Image</h4> 
        <input type="file" ng-model="post.primaryImage" /> 
    
    </md-input-container> 
    

    控制器:

    var url = '/Users/my.name/Desktop/images' 
    
    
        $scope.OnSubmit = function() { 
        //ResourceService.addMachine.save($scope.post); 
        console.log($scope.post); 
        $scope.upload(file); 
        } 
    
    
        $scope.upload = function (file) { 
        Upload.upload({ 
         url: 'url', 
         data: {file: file} 
        }).then(function (resp) { 
         console.log('Success ' + resp.data); 
        }); 
    
    
        }  
    

    逸岸的圖像是沒有得到輸入。 當我打印scope.post控制檯上打印一個空白對象。 我已經閱讀了關於這個主題的幾個答案,但是關於如何上傳本地系統文件夾中的圖像沒有任何回答的問題。

    回答

    1

    嘗試使用ngf-select這樣的:

    HTML

    <div ng-controller="MyCtrl"> 
        <input type="file" ngf-select="onFileSelect($files)" multiple> 
    </div> 
    

    控制器

    angular.module('myApp', ['ngFileUpload']); 
    
    var MyCtrl = ['$scope', 'Upload', function($scope, Upload) { 
        $scope.onFileSelect = function($files) { 
         for (var i = 0; i < $files.length; i++) { 
          var $file = $files[i]; 
          Upload.upload({ 
          url: 'my/upload/url', 
          file: $file, 
          }) 
         } 
        } 
        }]; 
    

    這裏是jsFiddle

    希望它有效:)

    +0

    非常感謝。圖像正在獲取輸入。唯一的問題是它不會上傳到「/Users/my.name/Desktop/images」。 localhost:8888/Users/my.name/Desktop/images 404(Not Found)。我想要我的系統上的圖像。我應該提及哪些網址。 – MasterMind

    相關問題