Search code examples
angularjsfile-uploadplayframework-2.0asyncfileuploadangular-file-upload

handling file uploads sent via xhr using play framework v2.4


I am using play framework v2.4 and am testing file uploads sent via a web form (xhr call via angular). I have tried the following two options in the play framework controller (based on https://www.playframework.com/documentation/2.4.x/JavaFileUpload) but neither option works (I also added the curl sample I tried but I am unable to get a reference to the file - its always null). The MultipartFormData/File is null in either case.
Thanks

MultipartFormData body = request().body().asMultipartFormData();

or

File file = request().body().asRaw().asFile();

The curl command I am using is as follows (I can see the file data being sent from curl):

curl -v --cookie "Auth-Token=dc6eb764-4436-4571-b4bf-8cf5dg36b71b" -F "doc=@/home/test/docs/books/play2.pdf" https://local.test.com/a/api/contact/abc/test

Solution

  • the issue was with the client side JS code that was not uploading the file. The following worked:

    MultipartFormData body = request().body().asMultipartFormData();
    

    test with curl

    curl -v --cookie "Auth-Token=dc6eb764-4436-4571-b4bf-8cf5dg36b71b" -F "doc=@/home/test/docs/books/play2.pdf" https://local.test.com/a/api/contact/abc/test