开发者

How do you check the mimetype of a file uploaded to your server?

开发者 https://www.devze.com 2023-01-24 16:55 出处:网络
it\'s just a user u开发者_开发知识库pload a file.UploadedFile.content_type will return the content-type header that was sent with the file when uploaded at the time of upload.

it's just a user u开发者_开发知识库pload a file.


UploadedFile.content_type will return the content-type header that was sent with the file when uploaded at the time of upload.

If you also need to check files after they are saved you can use the mimetypes module in python. But it appears to only check based on the file extension.

import mimetypes
file_type, file_encoding = mimetypes.guess_type('/path/to/file')
print 'File-type: %s\nFile-encoding: %s' % (file_type, file_encoding)

And if you have file-type requirements that are not detected by default you can add the types to mimetypes simply too before using guess_type:

mimetypes.add_type('font/ttf', '.ttf')


UploadedFile.content_type

Check http://docs.djangoproject.com/en/dev/topics/http/file-uploads/?from=olddocs for more info

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号