【发布时间】:2019-12-24 05:40:57
【问题描述】:
我正在尝试使用 ajax 将图像文件放入 Django views.py。 我可以打印它的文件名和大小。但是当我打开文件时,它变成了 None..!
我尝试打开图像的原因是为了将其用于视觉分析。 目前我正在开发 Django 1.11、python 3.6.7。
这里有一些代码。
views.py
def receipt_ocr(request):
if request.method == 'POST':
print(type(request.FILES["file"])) #<class 'django.core.files.uploadedfile.InMemoryUploadedFile'>
print(request.FILES["file"]) #bank.png
print(request.FILES["file"].size) #119227
print(request.FILES["file"].name) #bank.png
print(request.FILES["file"].content_type) #image/png
print(type(request.FILES["file"].open())) #<class 'NoneType'>
print(request.FILES["file"].open()) #None
print(type(request.FILES["file"].read())) #<class 'bytes'>
imageFile = request.FILES["file"]
receipt_image = imageFile.read()
print(type(receipt_image)) #<class 'bytes'>
print(receipt_image) #b''
html中的ajax部分
$.ajax({
url: "{% url 'ocr:receipt_ocr' %}",
enctype: 'multipart/form-data',
type: 'POST',
cache: false,
processData: false,
contentType: false,
data: postData,
complete: function(req){
alert('good');
},
error: function(req, err){
alert('message:' + err);
}
});
我预计views.py 中的receipt_image 是图像文件的二进制文件。但它是空的。因为 request.FILES["file"] 是无。我不知道为什么它是无。
感谢您阅读我的问题。 希望你有美好的一天????
【问题讨论】:
标签: python django ajax forms python-requests