2015-02-12 2 views
1

Я сделал загрузку весеннего остатка angularjs по следующему коду, он хорошо работает и сохраняет файл, теперь я хочу добавить текст ввода, например описание в html-форму, с загрузкой файла и сохранить их. Каков код Я должен добавить в formData() в угловой контроллер, чтобы сделать это?angularjs spring rest file upload form

angularjs Контроллер

var myDropzone = new Dropzone("div#file", 
       { url: "#" 
       }); 
      myDropzone.on("addedfile", function(file) { 
       $scope.file=file; 

      }); 
    $scope.uploadFile=function(){ 
     var formData=new FormData(); 
     formData.append("file",$scope.file); 
     console.log(formData) 
     $http.post('/pagingpoc/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); 
      }); 
     $('#saveAttachmentModal').modal('hide'); 
    }; 

HTML форма

<form ng-submit="uploadFile()" enctype="multipart/form-data"> 

     <div class="form-group"> 
      <label>Description</label> 
      <textarea rows="5" cols="5" ng-model="description"></textarea><br> 
     </div> 

     <div id="file" class="dropzone"></div> 
     <div class="modal-footer"> 
     <button type="button" class="btn btn-default" 
        data-dismiss="modal" ng-click="clear()"> 
       <span class="glyphicon glyphicon-ban-circle"></span> Cancel 
         </button> 
      <button type="submit" class="btn btn-primary"> 
         <span class="glyphicon glyphicon-save"></span> Save 
         </button> 
     </div> 
    </form> 

Остальной Контроллер

 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); 
     attachmentRepository.save(attachment); 
     File dir = new File("/home/ali/"); 
     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("Error Found"); 
     } 

} 
+0

заголовки: {ENCTYPE: 'многочастному/форм-данных'} внутри вашего поста –

ответ

1

Контроллер:

var myDropzone = new Dropzone("div#file", { url: "#"}); 
myDropzone.on("addedfile", function(file) { 
    $scope.file=file; 
}); 
$scope.text = ''; 
$scope.uploadFile = function(){ 
    var formData=new FormData(); 
    formData.append("file",$scope.file); 
    formData.append("text",$scope.text); //here 
    $http.post('/pagingpoc/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); 
      }); 
     $('#saveAttachmentModal').modal('hide'); 
    }; 

Хмлт:

Описание
Отменить Сохранить