Thạ*_*yên 4 python mime-types python-magic
如果文件大小低于 5000 字节 (InMemoryUploadedFile)。这段代码不起作用
mime_type = magic.from_buffer(file.read(), mime=True)
Run Code Online (Sandbox Code Playgroud)
它返回错误的 mime_type。例如,我有一个大小为 4074 字节的文件 cv.docx。它返回一个 mime_type:
'application/x-empty'
Run Code Online (Sandbox Code Playgroud)
代替
'application/vnd.openxmlformats-officedocument.wordprocessingml.document'
Run Code Online (Sandbox Code Playgroud)
您能给我一些解决此案的建议吗?
我也有这个问题。这很可能与文件大小无关,因为我也在 90 字节文本/纯文件上测试了 magic.from_buffer 并且它返回了正确的值。问题是该文件不知何故变空了。就我而言,这是因为该文件是一个流,并且我已经从流中读取(请记住,如果您从流中读取并再次读取,第二次读取将从第一次读取完成的位置开始 - 与从开始处读取不同)每次一个文件)。这个例子来自烧瓶
mime_type1 = magic.from_buffer(request.stream.read(2048), mime=True) // returns text/plain
mime_type = magic.from_buffer(request.files["file"].stream.read(2048), mime=True) // returns application/x-empty because the stream has already been read from
Run Code Online (Sandbox Code Playgroud)
如果不查看之前的代码,很难准确诊断,但请检查您在其他位置使用该文件并将其注释掉。你可能需要做类似的事情
file.seek(0)
mime_type = magic.from_buffer(file.read(), mime=True)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1652 次 |
| 最近记录: |