File Upload using AngularJS

前端 未结 29 2030
野趣味
野趣味 2020-11-21 07:24

Here is my HTML form:

相关标签:
29条回答
  • 2020-11-21 08:17

    http://jsfiddle.net/vishalvasani/4hqVu/ works fine in chrome and IE (if you update CSS a little in background-image). This is used for updating progress bar:

     scope.progress = Math.round(evt.loaded * 100 / evt.total)
    

    but in FireFox angular's [percent] data is not updated in DOM successfully,although files are uploading successfully.

    0 讨论(0)
  • 2020-11-21 08:17

    Working Example using Simple Directive (ng-file-model):

    .directive("ngFileModel", [function () {
      return {
          $scope: {
              ngFileModel: "="
          },
          link: function ($scope:any, element, attributes) {
              element.bind("change", function (changeEvent:any) {
                  var reader = new FileReader();
                  reader.onload = function (loadEvent) {
                      $scope.$apply(function () {
                          $scope.ngFileModel = {
                              lastModified: changeEvent.target.files[0].lastModified,
                              lastModifiedDate: changeEvent.target.files[0].lastModifiedDate,
                              name: changeEvent.target.files[0].name,
                              size: changeEvent.target.files[0].size,
                              type: changeEvent.target.files[0].type,
                              data: changeEvent.target.files[0]
                          };
                      });
                  }
                  reader.readAsDataURL(changeEvent.target.files[0]);
              });
          }
      }
    }])
    

    and use FormData to upload file in your function.

    var formData = new FormData();
     formData.append("document", $scope.ngFileModel.data)
     formData.append("user_id", $scope.userId)
    

    all credits go for https://github.com/mistralworks/ng-file-model

    I have faced a small probelm you can check it here: https://github.com/mistralworks/ng-file-model/issues/7

    Finally,here's a forked repo: https://github.com/okasha93/ng-file-model/blob/patch-1/ng-file-model.js

    0 讨论(0)
  • 2020-11-21 08:17

    UPLOAD FILES

    <input type="file" name="resume" onchange="angular.element(this).scope().uploadResume()" ng-model="fileupload" id="resume" />
    
    
            $scope.uploadResume = function () { 
                var f = document.getElementById('resume').files[0];
                $scope.selectedResumeName = f.name;
                $scope.selectedResumeType = f.type;
                r = new FileReader();
    
                r.onloadend = function (e) { 
                    $scope.data = e.target.result;
                }
    
                r.readAsDataURL(f);
    
            };
    

    DOWNLOAD FILES:

              <a href="{{applicant.resume}}" download> download resume</a>
    
    var app = angular.module("myApp", []);
    
                app.config(['$compileProvider', function ($compileProvider) {
                    $compileProvider.aHrefSanitizationWhitelist(/^\s*(https?|local|data|chrome-extension):/);
                    $compileProvider.imgSrcSanitizationWhitelist(/^\s*(https?|local|data|chrome-extension):/);
    
                }]);
    
    0 讨论(0)
  • 2020-11-21 08:18

    i think this is the angular file upload:

    ng-file-upload

    Lightweight Angular JS directive to upload files.

    Here is the DEMO page.Features

    • Supports upload progress, cancel/abort upload while in progress, File drag and drop (html5), Directory drag and drop (webkit), CORS, PUT(html5)/POST methods, validation of file type and size, show preview of selected images/audio/videos.
    • Cross browser file upload and FileReader (HTML5 and non-HTML5) with Flash polyfill FileAPI. Allows client side validation/modification before uploading the file
    • Direct upload to db services CouchDB, imgur, etc... with file's content type using Upload.http(). This enables progress event for angular http POST/PUT requests.
    • Seperate shim file, FileAPI files are loaded on demand for non-HTML5 code meaning no extra load/code if you just need HTML5 support.
    • Lightweight using regular $http to upload (with shim for non-HTML5 browsers) so all angular $http features are available

    https://github.com/danialfarid/ng-file-upload

    0 讨论(0)
  • 2020-11-21 08:19

    Below is working example of file upload:

    http://jsfiddle.net/vishalvasani/4hqVu/

    In this one function called

    setFiles
    

    From View which will update the file array in controller

    or

    You can check jQuery File Upload using AngularJS

    http://blueimp.github.io/jQuery-File-Upload/angularjs.html

    0 讨论(0)
  • 2020-11-21 08:19

    We have used HTML, CSS and AngularJS. Following example shows about how to upload the file using AngularJS.

    <html>
    
       <head>
          <script src = "https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
       </head>
    
       <body ng-app = "myApp">
    
          <div ng-controller = "myCtrl">
             <input type = "file" file-model = "myFile"/>
             <button ng-click = "uploadFile()">upload me</button>
          </div>
    
          <script>
             var myApp = angular.module('myApp', []);
    
             myApp.directive('fileModel', ['$parse', function ($parse) {
                return {
                   restrict: 'A',
                   link: function(scope, element, attrs) {
                      var model = $parse(attrs.fileModel);
                      var modelSetter = model.assign;
    
                      element.bind('change', function(){
                         scope.$apply(function(){
                            modelSetter(scope, element[0].files[0]);
                         });
                      });
                   }
                };
             }]);
    
             myApp.service('fileUpload', ['$http', function ($http) {
                this.uploadFileToUrl = function(file, uploadUrl){
                   var fd = new FormData();
                   fd.append('file', file);
    
                   $http.post(uploadUrl, fd, {
                      transformRequest: angular.identity,
                      headers: {'Content-Type': undefined}
                   })
    
                   .success(function(){
                   })
    
                   .error(function(){
                   });
                }
             }]);
    
             myApp.controller('myCtrl', ['$scope', 'fileUpload', function($scope, fileUpload){
                $scope.uploadFile = function(){
                   var file = $scope.myFile;
    
                   console.log('file is ' );
                   console.dir(file);
    
                   var uploadUrl = "/fileUpload";
                   fileUpload.uploadFileToUrl(file, uploadUrl);
                };
             }]);
    
          </script>
    
       </body>
    </html>
    
    0 讨论(0)
提交回复
热议问题