【问题标题】:Angular Js File UploadAngular Js 文件上传
【发布时间】:2013-12-22 00:33:25
【问题描述】:

我是 Angular js 的新手。我想用这个上传文件。 我使用了以下链接

http://jsfiddle.net/jasonals/WEvwz/27/? 上传.html

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script src="http://blueimp.github.com/jQuery-File-Upload/js/vendor/jquery.ui.widget.js"></script>
<script src="http://blueimp.github.com/jQuery-File-Upload/js/jquery.fileupload.js"></script>
<div ng-controller='TestCtrl'>
  <div>
      <input id="fileupload" type="file" name="files[]" data-url="/" multiple>
      <ul>
          <li ng-repeat="file in fileList">
              {{file.name}}
          </li>
      </ul>
  </div>

  <button ng-click='addButtonClicked()'>Add File</button>
</div>

controlller file.
$(document).ready(function(){
    $('#fileupload').fileupload({
        dataType: 'json'});
});

TestCtrl = function($scope){
    $scope.fileList = [];
    $('#fileupload').bind('fileuploadadd', function(e, data){
        // Add the files to the list
        numFiles = $scope.fileList.length
        for (var i=0; i < data.files.length; ++i) {
            var file = data.files[i];
        // .$apply to update angular when something else makes changes
        $scope.$apply(
            $scope.fileList.push({name: file.name})
            );
        }
        // Begin upload immediately
        data.submit();
    });
    $scope.addButtonClicked = function(){
        var numFiles = $scope.fileList.length;
        $scope.fileList.push({name: ('fileName' + numFiles)});
    }
}

但我无法使用此发布网址。

【问题讨论】:

    标签: jquery django angularjs


    【解决方案1】:

    我无法直接回答您的问题,只能说:确保您在 JSFiddle 示例的输入中设置了 data-url

    作为替代方案,我会推荐angular-file-upload,我已经成功使用它。它使用角度指令来完成上传。这种模式使用了更多的代码,但将应用程序中的关注点分开,因此您的控制器只是将事物粘合在一起,您的服务实际上与外部世界对话。

    这将使您的应用更易于测试:

    <div ng-controller="FileUploadController">
      <h3>Upload a file</h3>
      <input type="file" ng-file-select="uploadFile($files)" />
    </div>
    

    还有 javascript:

    // define the app, include the file upload dependency
    var app = angular.module('MyApp', ['ng', 'angularFileUpload']);
    
    // controller to handle the file upload event
    app.controller('FileUploadController', 
      function ($scope, $rootScope, FileUploadService) {
        var service = FileUploadService;
        /** 
         *  Handler to upload a new file to the server.
         */
        $scope.uploadFile = function ($files) {
          var $file = $files[0];
          service.uploadFile($file, function (error) {
            if (error) {
              alert('There was a problem uploading the file.');
            }
            // handle successfully-uploaded file
          })
        }
      });
    
    // services should interact with the outside world
    app.factory('FileUploadService', function ($http) {
      var api = {
        uploadFile: function (file, callback) {
          $http.uploadFile({
            url: '/some/remote/end/point/',
            file: file
          }).progress(function(event) {
            console.log('percent: ' + parseInt(100.0 * event.loaded / event.total));
          }).error(function (data, status, headers, config) {
            console.error('Error uploading file')
            callback(status);
          }).then(function(data, status, headers, config) {
            callback(null);
          });
        }
      }
      return api;
    });
    

    【讨论】:

    • 谢谢。这是我真正想要的。真的它工作正常。感谢您的帮助。
    【解决方案2】:

    试试这个...

    $(document).ready(function(){
        $('#fileupload').fileupload({
            dataType: 'json'});
    });
    
    TestCtrl = function($scope){
        $scope.fileList = [];
        $('#fileupload').bind('fileuploadadd', function(e, data){
                $scope.$apply(
                $scope.fileList.push({name: file.name})
                );   
            data.submit();
        });
    
    }
    

    【讨论】:

    • 感谢您的回答这是我所需要的。
    猜你喜欢
    • 2016-08-20
    • 2016-04-21
    • 2014-03-02
    • 2015-10-25
    • 2017-07-26
    • 1970-01-01
    • 2017-01-24
    • 2015-12-06
    • 1970-01-01
    相关资源
    最近更新 更多