Django Rest框架:通过AJAX上传文件 [英] Django Rest Framework: Upload file via AJAX
问题描述
我有一个视图和序列化程序:
I have a view and serializer:
class UserView(generics.RetrieveUpdateAPIView):
model = get_user_model()
serializer_class = UserProfileSerializer
permission_classes = (permissions.IsAuthenticated,)
def get_object(self, *args, **kwargs):
return self.request.user
class UserImageSerializer(serializers.ModelSerializer):
class Meta:
model = get_user_model()
fields = ('image',)
他们对httpie很好:
They work great with httpie:
http -f put localhost:8000/accounts/api/image/ "Authorization: Token mytoken" image@~/Downloads/test.jpg
HTTP/1.0 200 OK
Allow: GET, PUT, PATCH, HEAD, OPTIONS
Content-Type: application/json
Date: Thu, 03 Sep 2015 22:50:33 GMT
Server: WSGIServer/0.2 CPython/3.4.3
Vary: Accept
X-Frame-Options: SAMEORIGIN
{
"image": "http://localhost:8000/media/accounts/user_images/test.jpg"
}
,我的图片已上传,
现在我想要使用AJAX上传文件,但显然不想使用:
Now I want to be able to upload a file using AJAX, but it apparently doesn't want to work:
<form action="http://localhost:8000/accounts/api/image/"
method="put"
enctype="multipart/form-data">
<input name="image" type="file">
<input type="submit">
</form>
<script src="https://code.jquery.com/jquery-2.1.4.min.js"></script>
<script>
$('form').submit(function(e) {
var formData = new FormData($(this));
$.ajax({
url: $(this).attr('action'),
type: $(this).attr('method'),
data: formData,
headers: {'Authorization': 'Token mytoken'},
cache: false,
contentType: false,
processData: false,
success: function() { alert('it works') },
});
e.preventDefault();
});
</script>
现在,我的工作警告出现。我知道表单正在提交到正确的地方,我可以在Django开发者服务器中看到它被要求为PUT,并且响应200(与httpie相同的响应):
Now, my "It works" alert appears. I know the form is being submitted to the right place, I can see in the Django dev server that it's being requested as PUT and that it responds with 200 (same response as with httpie):
[03/Sep/2015 22:47:23] "PUT /accounts/api/image/ HTTP/1.1" 200 77
但似乎该文件未被上传,并且不会显示在管理员中。
But it seems like the file isn't being uploaded, and it doesn't show up in the admin.
我没有想法。
推荐答案
好的,我不能完全解释为什么,但是看起来像 var formData = new FormData($(this));
不够单独,需要明确附加,因为原因?如果有人可以解释,请做。
Ok, I can't exactly explain why, but it seems like var formData = new FormData($(this));
is not enough alone, it needs to be explicitly appended, because reasons? If anyone can explain, please do.
工作代码:
<form action="http://localhost:8000/accounts/api/image/"
method="put"
enctype="multipart/form-data">
<input name="image" type="file" id="image">
<input type="submit">
</form>
<script src="https://code.jquery.com/jquery-2.1.4.min.js"></script>
<script>
$('form').submit(function(e) {
var formData = new FormData($(this));
formData.append('image', $('#image')[0].files[0]);
$.ajax({
url: $(this).attr('action'),
type: $(this).attr('method'),
data: formData,
headers: {'Authorization': 'Token mytoken'},
cache: false,
contentType: false,
processData: false,
success: function() { alert('it works') },
});
e.preventDefault();
});
</script>
这篇关于Django Rest框架:通过AJAX上传文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!