Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1001143
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T07:37:21+00:00 2026-05-16T07:37:21+00:00

I am using Flask micro-framework 0.6 and Python 2.6 I need to get the

  • 0

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'))
        else:
            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> 
</form> 

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T07:37:23+00:00Added an answer on May 16, 2026 at 7:37 am

    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'))
            else:
                return redirect(url_for('upload'))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I see that we can try Flask (the python micro framework from the browser,
Possible Duplicate: Get ip address of visitors using Python (specifically Flask micro-framework) I'm creating
I am using Flask and I return an XML file from a get request.
I am using the Flask micro-framework which is based on Werkzeug, which uses Python.
Im using the python framework flask with sqlalchemy. My many-to-many looks like this: collections_questions
I'm making a new website to replace a current one, using Flask micro-framework (based
I'm generating an HTML page via Python, using the Flask framework to receive messages
I'm using Flask-WTF: Here is my form: from flask.ext.wtf import Form, TextField class BookNewForm(Form):
I'm using Flask framework with sqlalchemy and I can't query some related table of
I'm working in Python and using Flask. When I run my main Python file

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.