TIMEX
TIMEX

Reputation: 271614

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

it's just a user upload a file.

Upvotes: 0

Views: 1737

Answers (2)

AgentK
AgentK

Reputation: 685

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')

Upvotes: 3

Alxandr
Alxandr

Reputation: 12423

UploadedFile.content_type

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

Upvotes: 2

Related Questions