File Upload using AngularJS

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

Here is my HTML form:

29条回答
  •  孤城傲影
    2020-11-21 08:01

    app.directive('ngUpload', function () {   
      return {    
        restrict: 'A',  
        link: function (scope, element, attrs) {
    
          var options = {};
          options.enableControls = attrs['uploadOptionsEnableControls'];
    
          // get scope function to execute on successful form upload
          if (attrs['ngUpload']) {
    
            element.attr("target", "upload_iframe");
            element.attr("method", "post");
    
            // Append a timestamp field to the url to prevent browser caching results
            element.attr("action", element.attr("action") + "?_t=" + new Date().getTime());
    
            element.attr("enctype", "multipart/form-data");
            element.attr("encoding", "multipart/form-data");
    
            // Retrieve the callback function
            var fn = attrs['ngUpload'].split('(')[0];
            var callbackFn = scope.$eval(fn);
            if (callbackFn == null || callbackFn == undefined || !angular.isFunction(callbackFn))
            {
              var message = "The expression on the ngUpload directive does not point to a valid function.";
              // console.error(message);
              throw message + "\n";
            }                      
    
            // Helper function to create new  i frame for each form submission
            var addNewDisposableIframe = function (submitControl) {
              // create a new iframe
              var iframe = $("