开发者

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

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜