简体   繁体   English

使用AngularJS和Spring Rest拖动文件上传

[英]Drag Drop file upload with AngularJS and Spring Rest

I have angularjs And spring rest file upload it work well but i need to change file upload in html file to dropzone.js or any drag drop file upload,I tried dropzone.js library but I couldn't integrate it with angular ,Can any one help me how can i do that? 我有angularjs和Spring rest文件上传它工作得很好,但我需要将文件上传到html文件中dropzone.js或任何拖放文件上传,我试过dropzone.js库但我无法将它与角度集成,可以任何一个人帮我,我该怎么做?

Angularjs controller Angularjs控制器

   $scope.document = {};
   $scope.setTitle = function(fileInput) {
   var file=fileInput.value;
   var filename = file.replace(/^.*[\\\/]/, '');
   var title = filename.substr(0, filename.lastIndexOf('.'));
   $("#title").val(title);
   $("#title").focus();
   $scope.document.title=title;
 };


 $scope.uploadFile=function(){
        var formData=new FormData();
        formData.append("file",file.files[0]);
        $http.post('/app/newDocument', formData, {
            transformRequest: function(data, headersGetterFunction) {
                return data;
            },
            headers: { 'Content-Type': undefined }
            }).success(function(data, status) {                       
                console.log("Success ... " + status);
            }).error(function(data, status) {
                console.log("Error ... " + status);
            });
  };
 });

html form HTML表单

  <form ng-submit="uploadFile()" class="form-horizontal"
              enctype="multipart/form-data">
 <input type="file" name="file" ng-model="document.fileInput" id="file" />
 <input type="text" class="col-sm-4" ng-model="document.title" id="title" />
    </form>

Rest Controller 休息控制器

@RequestMapping(value="/newDocument", method = RequestMethod.POST)
    public void UploadFile(MultipartHttpServletRequest request,
   HttpServletResponse response) throws IOException {

        Attachment attachment=new Attachment();
        Iterator<String> itr=request.getFileNames();
        MultipartFile file=request.getFile(itr.next());
        String fileName=file.getOriginalFilename();
        attachment.setName(fileName);
        File dir = new File("D:\\file");
         if (dir.isDirectory())
         {
            File serverFile = new File(dir,fileName);
           BufferedOutputStream stream = new BufferedOutputStream(
                 new FileOutputStream(serverFile));
           stream.write(file.getBytes());
           stream.close();
       }else {
        System.out.println("not");
      }

 }

I would personally use a dedicated directive such as the excellent: 我个人会使用一个专门的指令,例如优秀:

https://github.com/danialfarid/angular-file-upload https://github.com/flowjs/ng-flow https://github.com/danialfarid/angular-file-upload https://github.com/flowjs/ng-flow

These take care of the boilerplate code and let you focus on styling and creating an upload service that works in sync with your API. 它们负责样板代码,让您专注于样式设置并创建与API同步的上传服务。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM