Flask / Python. Get mimetype from downloaded file

I am using flash micro framework 0.6 and Python 2.6

I need to get mimetype from the downloaded file so that I can save it.

Here is the corresponding 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 loads. I looked at the Flask docs here: http://flask.pocoo.org/docs/api/#incoming-request-data
Therefore, I know that it gets the mimetype type, but I cannot figure out how to get it - like a text string, for example. 'Txt / plain.

Any ideas?

Thanks.

+7
python flask webforms
source share
1 answer

From docs , file.content_type contains the full encoding type, mimetype contains only 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')) 
+17
source share

All Articles