Flask / Python. Get mimetype from uploaded file


I am using Flask micro-framework 0.6 and Python 2.6

I need to get the mimetype from an uploaded file so I can store it.

Here is the relevent Python/Flask code:

@app.route('/upload_file', methods=['GET', 'POST'])
def upload_file():
    if request.method == 'POST':
        file = request.files['file']
        mimetype = #FIXME
        if file:
            file.save(os.path.join(UPLOAD_FOLDER, 'File-Name')
            return redirect(url_for('uploaded_file'))
            return redirect(url_for('upload'))

And here is the code for the webpage:

<form action="upload_file" method=post enctype=multipart/form-data> 
Select file to upload: <input type=file name=file> 
<input type=submit value=Upload> 

The code works, but I need to be able to get the mimetype when it uploads. I've had a look at the Flask docs here: http://flask.pocoo.org/docs/api/#incoming-request-data
So I know it does get the mimetype, but I can't work out how to retrieve it - as a text string, e.g. 'txt/plain'.

Any ideas?

Thank you.

8/10/2010 10:14:01 AM

Accepted Answer

From the docs, file.content_type contains the full type with encoding, mimetype contains just the mime type.

@app.route('/upload_file', methods=['GET', 'POST'])
def upload_file():
    if request.method == 'POST':
        file = request.files.get('file')
        if file:
            mimetype = file.content_type
            filename = werkzeug.secure_filename(file.filename)
            file.save(os.path.join(UPLOAD_FOLDER, filename)
            return redirect(url_for('uploaded_file'))
            return redirect(url_for('upload'))
3/24/2017 1:43:41 PM

You could in theory use request.files['YOUR_FILE_KEY'].content_type, but the implementation (included below, found in werkzeug.datastructures) either trust whatever the client is providing, or uses mimetypes.guess_type which only checks the file extension (see Python doc here).

class FileMultiDict(MultiDict):

    """A special :class:`MultiDict` that has convenience methods to add
    files to it.  This is used for :class:`EnvironBuilder` and generally
    useful for unittesting.
    .. versionadded:: 0.5

    def add_file(self, name, file, filename=None, content_type=None):
        """Adds a new file to the dict.  `file` can be a file name or
        a :class:`file`-like or a :class:`FileStorage` object.
        :param name: the name of the field.
        :param file: a filename or :class:`file`-like object
        :param filename: an optional filename
        :param content_type: an optional content type
        if isinstance(file, FileStorage):
            value = file
            if isinstance(file, string_types):
                if filename is None:
                    filename = file
                file = open(file, 'rb')
            if filename and content_type is None:
                content_type = mimetypes.guess_type(filename)[0] or \
            value = FileStorage(file, filename, name, content_type)

        self.add(name, value)

Depending on your use case, you might want to use python-magic which will use the actual file to get the mimetype. It would something like that:

import magic

def get_mimetype(data: bytes) -> str:
    """Get the mimetype from file data."""
    f = magic.Magic(mime=True)
    return f.from_buffer(data)


Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow