Django ajax file upload

后端 未结 2 1475
迷失自我
迷失自我 2021-01-01 08:39

So I am trying to upload a file without any external plugins, but I am running into some errors.

                
2条回答
  •  感动是毒
    2021-01-01 09:14

    i followed this tutorial http://www.script-tutorials.com/pure-html5-file-upload/ and in the php part i replaced with :

    class UploadImageView(LoginRequiredMixin, CurrentUserIdMixin, View):
    
        @method_decorator(csrf_protect)
        def dispatch(self, *args, **kwargs):
            return super(UploadImageView, self).dispatch(*args, **kwargs)
    
        def post(self, request, username):
            path = 'myproject/media/pictures/guitar.jpg'
            f = request.FILES['image_file']
            destination = open(path, 'wb+')
            for chunk in f.chunks():
                destination.write(chunk)
                destination.close()
    
                return HttpResponse("image uploaded")
    

    also changed this lines

    
                                    {% csrf_token %}
    

提交回复
热议问题