0

I am using ng-file-upload AngularJs API to upload multiple files to server.But this is the traditional way to do it.But my requirement is that i dont need to store files in a server as it is.I have a REST end point that responsible for store user input data to DB.Along with the REST request i pass the file Array object with other forms values.When data comes to REST end point it access each attributes and store data.When it tried to read File Array obj i can not read the file content for each file.

Sample File Upload Code

jsfiddle

Note that i just want to pass only $scope.files along with the REST request.Please let me know how can i read file content values from server side reading file Array in Java.If you guys know any better way to do this please share your ideas.

REST Service Code Snippet

@POST
@Path("/manual")
@Produces(MediaType.APPLICATION_JSON)
public boolean insertResults(testVO testResult) {

    for(Object o:testVO.getFiles()){

        LinkedHashMap<String, String> l=(LinkedHashMap<String, String>) o;
        System.out.println(l.get("result"));    
    }
}

Note: testVO.getFiles() type is Object[] array.

In my preceding code i convert object into LinkedHashMap and access the necessary fields like size,type,etc.But my requirement is that how can i get the content belong to that file.

gihan-maduranga
  • 4,381
  • 5
  • 41
  • 74
  • post your server side code, the problem is server side, client side seems fine. – danial Jan 25 '16 at 06:39
  • @danial i have updated my post.plz have a look.thanks. – gihan-maduranga Jan 25 '16 at 06:56
  • @dania FYI http://stackoverflow.com/questions/34991498/how-to-send-list-of-files-array-along-with-the-form-data – gihan-maduranga Jan 25 '16 at 11:30
  • 1
    Your server code should look like this: https://spring.io/guides/gs/uploading-files/ if you are using Spring mvc. You need to read multipart form data. `public @ResponseBody String handleFileUpload(@RequestParam("file") MultipartFile file){...}` – danial Jan 25 '16 at 16:45
  • @danial is there any way to read file content and set as a attribute in `file.upload` method – gihan-maduranga Jan 25 '16 at 16:54
  • read the file content as what? the File object contains the file binary data, you can read the binary data as a byte array or convert it to base64 data url, but then you need to do the opposite conversion on the server side. Why would you want to do that? – danial Jan 25 '16 at 20:25
  • @danial becasue i dont store file in a server any physical location.in my server side code what does is get the necessary details from the file like size,name,type and content and do some mechanism and save in DB.(in there i convert content as a byte[]) .if i can send file content along with the request as a custom attribute it is easy for me to do my back end process. – gihan-maduranga Jan 26 '16 at 04:56
  • You can get the file's binary from MultipartFile – danial Jan 26 '16 at 08:22
  • @danial i was able to get files details in my server side.In file upload API i set java obj as a param value.`Upload.upload({ url: 'upload/url', data: {file: file, 'myobj': testVO} })` Please let me know how can i access those fields data in server side. – gihan-maduranga Jan 26 '16 at 08:25
  • read the forth comment for the server side code. – danial Jan 26 '16 at 20:43

0 Answers0