user534017
user534017

Reputation: 319

How does one obtain the type of an uploaded file in Django forms?

I have tried:

self.data['uploaded_file'].content_type

However, this gives an error that says the object lacks a content_type attribute.

Any ideas as to why? Thank you.

What is the best way to verify file type in Django forms?

Upvotes: 0

Views: 216

Answers (2)

yuwang
yuwang

Reputation: 876

How could you tell a file is binary file or text or some other stuff? Perhaps using the file extension is a way, but not always makes sense.

Upvotes: 0

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 798526

The content_type attribute is only present on the UploadedFile instances contained in request.FILES; you will need to get the content type from that, or use magic to get it from the raw file data.

Upvotes: 1

Related Questions