I have an API endpoint with Django Rest Framework to upload an image. Can you spot what I'm doing incorrectly?
我有一个带有Django Rest Framework的API端点来上传图像。你能发现我做错了什么吗?
#models.py
class test(models.Model):
...
upload_path = 'upload/'
image = models.ImageField(upload_to=upload_path, null=True, blank=True)
...
#serializers.py
class TestSerializer(serializers.ModelSerializer):
image = serializers.ImageField(
max_length=None, use_url=True,
)
class Meta:
model = test
fields = ('id','name','image',...)
#views.py
@api_view(['GET', 'POST'])
def test_list(request, site_id, block_id):
....
if request.method == 'POST':
serializer = TestSerializer(data=request.DATA)
if serializer.is_valid():
serializer.save()
return Response(serializer.data, status=status.HTTP_201_CREATED)
else:
return Response(
serializer.errors, status=status.HTTP_400_BAD_REQUEST)
else :
return Response(status=status.HTTP_403_FORBIDDEN)
#js
function setimage() {
var $input = $("#js_teaser_img");
var fd = new FormData;
fd.append('image', $input.prop('files')[0]);
$.ajax({
url: '/api/....',
data: fd,
processData: false,
contentType: false,
type: 'POST',
success: function (data) {
alert(data);
}
});
}
result image: ["No file was submitted."] 0: "No file was submitted."
结果图片:[“没有提交文件。”] 0:“没有提交文件。”
result
Django REST Framework upload image: "The submitted data was not a file"
Django REST Framework上传图片:“提交的数据不是文件”
+
var reader = new FileReader();
reader.onload = function(e) {
var img_local = e.target.result;
$('.js_img_src').attr('src', img_local);
$.post('/api/..../7/', {'image':img_local} , function( data ) {
console.log(data);
});
}
reader.readAsDataURL(file);
1 个解决方案
#1
1
From the client side in order to send files, you should use "multipart/form-data
" (jQuery sets contentType
as "application/x-www-form-urlencoded
" instead by default). Read this question on SO: Sending multipart/formdata with jQuery.ajax
从客户端发送文件,您应该使用“multipart / form-data”(jQuery将contentType设置为“application / x-www-form-urlencoded”,默认情况下)。在SO上阅读这个问题:使用jQuery.ajax发送multipart / formdata
Regarding instead python and django rest framework, you should use MultiPartParser
and/or FileUploadParser
in your API view and the preferred method for fle upload should be "put
", as you can see in the reference here: http://www.django-rest-framework.org/api-guide/parsers/#fileuploadparser.
关于python和django rest框架,你应该在你的API视图中使用MultiPartParser和/或FileUploadParser,并且应该“放置”文件上传的首选方法,正如你在这里的参考资料中所见:http://www.django- rest-framework.org/api-guide/parsers/#fileuploadparser。
ps. if you use django rest framework, I strongly encourage you to use Angular instead of jQuery, since it offers an excellent integration for rest services... trust me is FAR BETTER! ;)
PS。如果您使用django rest框架,我强烈建议您使用Angular而不是jQuery,因为它为休息服务提供了极好的集成...相信我更好! ;)
#1
1
From the client side in order to send files, you should use "multipart/form-data
" (jQuery sets contentType
as "application/x-www-form-urlencoded
" instead by default). Read this question on SO: Sending multipart/formdata with jQuery.ajax
从客户端发送文件,您应该使用“multipart / form-data”(jQuery将contentType设置为“application / x-www-form-urlencoded”,默认情况下)。在SO上阅读这个问题:使用jQuery.ajax发送multipart / formdata
Regarding instead python and django rest framework, you should use MultiPartParser
and/or FileUploadParser
in your API view and the preferred method for fle upload should be "put
", as you can see in the reference here: http://www.django-rest-framework.org/api-guide/parsers/#fileuploadparser.
关于python和django rest框架,你应该在你的API视图中使用MultiPartParser和/或FileUploadParser,并且应该“放置”文件上传的首选方法,正如你在这里的参考资料中所见:http://www.django- rest-framework.org/api-guide/parsers/#fileuploadparser。
ps. if you use django rest framework, I strongly encourage you to use Angular instead of jQuery, since it offers an excellent integration for rest services... trust me is FAR BETTER! ;)
PS。如果您使用django rest框架,我强烈建议您使用Angular而不是jQuery,因为它为休息服务提供了极好的集成...相信我更好! ;)