1

I have an angular view that is uploading files via the template

Multiple
<input type="file" name="file" nv-file-select="" uploader="uploader" multiple  /><br/>

Single
<input type="file" name="file" nv-file-select="" uploader="uploader" />

and the controller using an angular upload library (https://github.com/nervgh/angular-file-upload)

UploadController.$inject = ['$scope', 'FileUploader'];

 /**
 * @namespace UploadController
 */
 function UploadController($scope, FileUploader) {
   $scope.uploader = new FileUploader();
   $scope.uploader.url = 'api/v1/data/import/'
   $scope.uploader.method = 'PUT'

 };

On the server side the request is making it the right method in the View class

class FileUploadView(views.APIView):

   parser_classes = (FileUploadParser,)

   def put(self, request, format=None):

     file_obj = request.FILES['file']
     # ...
     # do some staff with uploaded file
     # ...
     return Response(status=204)

However the Dict that is created is empty

request.FILES['file']
(Pdb) *** django.utils.datastructures.MultiValueDictKeyError: "'file'"

It looks like there is parse exceptions built into the FileUploadParser that are not being run and that I am sending the right type of request over the wire. Why isnt there a way to tell why this request is not being parsed correctly? What am I missing?

1 Answer 1

1

First of all make sure that you know the difference between request.files and request.FILES. The former is for Django Rest Framework 2.x and the latter is for Django Rest Framework 3. As for the empty dict for some reason FileUploadParser was not parsing it correctly but switching the parser to MultipartParser did the trick. The enctype in the request was multipart so that might have something to do with it.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.