0

我想在Angular js/MVC中實現簡單的文件拖放功能。Angular js/MVC中的文件拖放功能不起作用

我創建了一個拖放指令。

(function (angular, undefined) { 
 
    'use strict'; 
 

 
    angular.module('fileupload', []) 
 
    .directive("myDirective", function ($parse) { 
 
     return { 
 
      restrict: 'A', 
 
      link: fileDropzoneLink 
 
     }; 
 
     function fileDropzoneLink(scope, element, attrs) { 
 
      element.bind('dragover', processDragOverOrEnter); 
 
      element.bind('dragenter', processDragOverOrEnter); 
 
      element.bind('dragend', endDragOver); 
 
      element.bind('dragleave', endDragOver); 
 
      element.bind('drop', dropHandler); 
 

 
      var onImageDrop = $parse(attrs.onImageDrop); 
 

 
      //When a file is dropped 
 
      var loadFile = function (files) { 
 
       scope.uploadedFiles = files; 
 
       scope.$apply(onImageDrop(scope)); 
 
      }; 
 

 
      function dropHandler(angularEvent) { 
 
       var event = angularEvent.originalEvent || angularEvent; 
 
       var files = event.dataTransfer.files; 
 
       event.preventDefault(); 
 
       loadFile(files) 
 
      } 
 

 
      function processDragOverOrEnter(angularEvent) { 
 
       var event = angularEvent.originalEvent || angularEvent; 
 
       if (event) { 
 
        event.preventDefault(); 
 
       } 
 
       event.dataTransfer.effectAllowed = 'copy'; 
 
       element.addClass('dragging'); 
 
       return false; 
 
      } 
 

 
      function endDragOver() { 
 
       element.removeClass('dragging'); 
 
      } 
 
     } 
 
    }); 
 
}(angular));

這是模板

<div class="dropzone" data-my-Directive on-image-drop="$ctrl.fileDropped()"> 
 
Drag and drop pdf files here 
 
</div>

這是我的組件代碼

(function (angular, undefined) { 
 
    'use strict'; 
 

 
    angular.module('test', []) 
 
     .component('contactUs', contactUs()); 
 

 
    function contactUs() { 
 
     ContactUs.$inject = ['$scope', '$http']; 
 
     function ContactUs($scope, $http) { 
 
      var ctrl = this; 
 
      ctrl.files = []; 
 
      
 
      ctrl.services = { 
 
       $scope: $scope, 
 
       $http: $http, 
 
      }; 
 
     } 
 

 
     //file dropped 
 
     ContactUs.prototype.fileDropped = function() { 
 
      var ctrl = this; 
 
      var files = ctrl.services.$scope.uploadedFiles; 
 
      angular.forEach(files, function (file, key) { 
 
       ctrl.files.push(file); 
 
      }); 
 
     } 
 

 

 
     return { 
 
      controller: ContactUs, 
 
      templateUrl: 'partials/home/contactus/' 
 
     }; 
 
    } 
 
}(angular));

有時拖放作品精絕沒有任何問題。但是有時候我會遇到下面的問題,拖放不起作用,我得到黑色的無效光標。

enter image description here

這個問題是隨機的,我沒有看到在控制檯中的任何錯誤。

而且我也嘗試像其他第三方組件角文件上傳 https://github.com/nervgh/angular-file-upload,我看到與該組件完全相同的問題也。

回答

0

編輯:

回答更新的PDF格式預覽。該代碼在同一個plunker中可用。

參考文獻:在https://stackoverflow.com/a/21732039/6347317

在上面的例子由@邁克爾優良的解決方案,從HTTP POST的響應中所用的「新斑點([反應]」以角文件上傳庫中,「。響應「將是」uploader.onAfterAddingFile「函數中的」fileItem._file「屬性。您可以通過控制檯日誌來檢查這些數據,以便更好地理解它。

也請注意,如果PDF閱讀器無法啓用Chrome,它必須使用此啓用:https://support.google.com/chrome/answer/6213030?hl=en

編輯完

既然你提到你試圖用角文件-upload圖書館,我創建了一個plunker它:

http://plnkr.co/edit/jeYg5fIRaC9wuEYSNOux?p=info

HTML:

<!DOCTYPE html> 
    <html ng-app="plunker"> 
     <head> 
     <meta charset="utf-8" /> 
     <title>AngularJS Plunker</title> 
     <script>document.write('<base href="' + document.location + '" />');</script> 
      <link rel="stylesheet" href="http://netdna.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" /> 
     <link rel="stylesheet" href="style.css" /> 
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script> 
     <script data-require="[email protected]" src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.11/angular.min.js" data-semver="1.5.11"></script> 
     <script src="http://netdna.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> 
     <script src="https://cdnjs.cloudflare.com/ajax/libs/angular-file-upload/2.5.0/angular-file-upload.min.js"></script> 
     <script src="app.js"></script> 
     </head> 

     <body ng-controller="MainCtrl"> 
         <div class="col-sm-4"> 

         <h3>Select files</h3> 

         <div ng-show="uploader.isHTML5"> 
          <!-- 3. nv-file-over uploader="link" over-class="className" --> 
          <div class="well my-drop-zone" style="width:300px" nv-file-drop nv-file-over="" uploader="uploader" 
          filters="syncFilter"> 
           <label> 
           Click here or Drag and Drop file 
           <input type="file" style="visibility:hidden;" nv-file-select nv-file-over="" uploader="uploader" 
           filters="syncFilter" multiple/> 
           </label> 
            <div class="progress" style="margin-bottom: 0;"> 
            <div class="progress-bar" role="progressbar" ng-style="{ 'width': uploader.queue[0].progress + '%' }"></div> 
            </div> 
           <div>{{uploader.queue[0].file.name}}</div> 

            <div ng-show="showAlert" class="alert alert-warning alert-dismissable"> 
            <a href="#" class="close" data-dismiss="alert" aria-label="close">×</a> 
            <strong>Clear the existing file before uploading again!!</strong> 
            </div> 
          </div> 

         </div> 
         </div> 

     </body> 

    </html> 

JS:

var app = angular.module('plunker', ['angularFileUpload']); 

app.controller('MainCtrl', function($scope,FileUploader) { 
var uploader = $scope.uploader = new FileUploader(); 

     // FILTERS 

     // a sync filter 
     uploader.filters.push({ 
      name: 'syncFilter', 
      fn: function(item /*{File|FileLikeObject}*/, options) { 
       console.log('syncFilter' + this.queue.length); 
       return this.queue.length < 1; 
      } 
     }); 

     // an async filter 
     uploader.filters.push({ 
      name: 'asyncFilter', 
      fn: function(item /*{File|FileLikeObject}*/, options, deferred) { 
       console.log('asyncFilter'); 
       setTimeout(deferred.resolve, 1e3); 
      } 
     }); 


uploader.allowNewFiles = true; 
uploader.filters.push({ 
    name:'csvfilter', 
    fn: function() { 
    return this.allowNewFiles; 
} 
}); 

     // CALLBACKS 

     uploader.onWhenAddingFileFailed = function(item /*{File|FileLikeObject}*/, filter, options) { 
      console.info('onWhenAddingFileFailed', item, filter, options); 
      $scope.showAlert=true; 
     }; 


     uploader.onAfterAddingFile = function(fileItem) { 

    }; 
}); 

這是很簡單的,我不給你提到的錯誤。我們實際上是在我們的一個項目中使用這個庫。我還添加了一個過濾器來限制上傳到只有1個文件。

請檢查這,讓我知道如何去或者如果你有代碼中的任何疑慮。

+0

感謝維傑但我仍然看到了同樣的問題,我注意到,當我嘗試運行的代碼在普通的HTML文件,並沒有VisualStudio中運行/ IIS中的文件被刪除的網頁上,它的作品,但我當在visual studio/iis中運行相同的代碼我看到上面的奇怪問題 –

+0

噢好吧。然後它可能是Visula studio中的一些設置。如何在Visual Studio中打開網頁?看到這個鏈接:https://stackoverflow.com/questions/15226600/visual-studio-does-not-let-me-drag-drop-items-into-it –

+0

不知道這是它,但你可以請看看在帖子底部的解決方案。可能只是Visual Studio的是Visual Studio中: https://www.devexpress.com/Support/Center/Question/Details/T520060/dxfileuploader-drag-and-drop-is-not-working-in-ie11-when -running-a-web-project-in-visual –