我使用Djnago 1.10.4框架上传pdf, 上传的文件返回django.core.files.uploadedfile.InMemoryUploadedFile的对象
当我尝试使用type(file_uploaded.file)从对象中提取数据时 我正进入(状态 宾语。 现在使用file_uploaded.file.read()。decode('utf-8')应该从pdf中提取数据。但我得到的是十六进制或某些东西的加密数据
if request.FILES:
file_uploaded = request.FILES["file"]
# file_uploaded = file.name
print('file_uploaded')
print(file_uploaded.file.read().decode('UTF-8', errors="ignore"))
print("file_uploaded.file", type(file_uploaded.file))
print("file_uploaded", type(file_uploaded))
file_type = ""
extn = file_uploaded.name
file_supported = [".pdf", ".jpg", ".png",".json", ".rtf", ".docx", ".txt", ".csv"]
extension = '.'+extn.split(".")[1]
# extension = "." + .split(".")[1]
if(extension in file_supported):
for formates in file_supported:
if (formates == extension):
file_type = formates[1:]
else:
print ('file is not uploaded')
上面的代码适用于文本文件,如csv,rft但不适用于pdf。 pdf文件的输出是这个
Β-'EMhXmt99*6":ȩ@+6H:yVl=d|9p
PeǸP|PRMYy+zR!ȷ/,yL=rD>lRM_QhOYO&|C'x.1%V]!pK,KEq㠪HSYO_O
{
4^R ς~7އG|䡚wgn˒( pOIUk]CF<~ (bt
譌)0vפ/HgDh+刃gzDU;$k-XOeki~kgۍJ_v41"
WS(X ]|GQyoZ?uu`^a;v(aiky|RUUSVT@P֯PIAZIW-)Ʌb?^mᇏ
UQbq3D@_J^Hdp7e,2̓\r|/e,vlBHG`-#!K<x1~i>s#cN7iL7QON-Wؖ
以下是上传csv文件时终端的输出:
file_uploaded
3526 HIGH ST,SACRAMENTO,95838,CA,2,1,836,Residential
file_uploaded.file <class '_io.BytesIO'>
file_uploaded
<class'django.core.files.uploadedfile.InMemoryUploadedFile'>
请帮我解决这个问题。