Django REST framework can't not implement file upload function can you help me?

4,026 views
Skip to first unread message

楊淯丰

unread,
Mar 21, 2016, 6:27:58 AM3/21/16
to Django REST framework
Hello Every one I have a problem.
I want to create file upload function in the class but the service request error message is “POST http://localhost:8000/eventfile/ 400 (Bad Request)” and “"Multipart form parse error - Invalid boundary in multipart: None””.


source code:
1.Django Service

class EventFileAPI(APIView):
   def post(self, request, format=None):

       print(self.request.data)
       return HttpResponse(JsonResponse({'event_data':'ok'}), content_type='application/json')

2.Front-End&AngularJS
HTML
...
<input type="file" file-model="myFile"/>
<button ng-click="uploadFile()">upload me</button>
...
angularjs
...
$http.post(uploadUrl, file, {
    transformRequest: angular.identity,
    headers: {
        'Content-Type': 'multipart/form-data',
        'Authorization': 'JWT '+$window.sessionStorage.getItem('accessToken')
    }
})
...

Cristiano Coelho

unread,
Mar 21, 2016, 8:32:34 AM3/21/16
to Django REST framework
Are you using a MultiPartParser on your API view? If so, is your form an actual enctype="multipart/form-data" form? If you are using angular, you problably need some additional angular library to send a file otherwise you will be probably using either json, or you probably need to use FileUploadParser instead.

Take a look at https://github.com/danialfarid/ng-file-upload and https://github.com/nervgh/angular-file-upload
Reply all
Reply to author
Forward
0 new messages