abhinav singh
abhinav singh

Reputation: 1104

Getting the absolute filename of file uploaded through Python Flask App

I am trying to create a flask app that can be used to upload any user selected file to my azure storage. For some reason, the mime-type of the uploaded file is always set to 'application/octet-stream'. If I directly upload the file to azure using its UI, then the mime-type is correct. To solve this problem, I am trying to manually calculate the mimetype of the file and pass it as metadata. The issue I am having is that I am not able to figure out a way to get the absolute filepath of the user selected file to be uploaded. enter image description here

What I am looking for is the absolute path: path/to/file/doctest2.txt

Here is how the flask app looks like:

@app.route('/', methods=['GET', 'POST'])
def upload_file():
    if request.method == 'POST':
        file = request.files['file']
        filename = secure_filename(file.filename)
        fileextension = filename.rsplit('.',1)[1]
        Randomfilename = id_generator()
        filename = Randomfilename + '.' + fileextension
        try:
            blob_service.create_blob_from_stream(container, filename, file)
        except Exception:
            print 'Exception=' + Exception 
            pass
        ref =  'http://'+ account + '.blob.core.windows.net/' + container + '/' + filename

seems like we can get the filename using f.filename, but I am not sure how to get the full path here. Complete code can be found here: https://github.com/codesagar/Azure-Blobs/blob/master/blob.py

The ultimate goal is to calculate the mimetype of the file to be uploaded. I do have the file-blob(variable f). IS there a better way to get the mime from blob rather than hunting for the absolute file-path?

Upvotes: 0

Views: 1125

Answers (1)

abhinav singh
abhinav singh

Reputation: 1104

I solved my problem by using the following line of code:

mime_type = f.content_type

This gives me the mimetype of the file and eliminates the need for getting the file's absolute path.

Upvotes: 1

Related Questions