Skip to content

Use of contentType inside send_file. #120

Closed
@corneauf

Description

@corneauf

As per the documentation here, the contentType field has been deprecated. However, it is still used here and here.

Currently, if someone saves an image through the gridfs module and use send_file it will leave the mimetype as text/html. Using the recommended method from the documentation of GridFS of putting the mimetype inside the metadata field will also not work.

Therefore, one must either pass contentType='image/base64' to the GridFS.put function or change the mimetype of the response_class object returned by send_file.

This behavior can only be noticed once you look at the sources. Therefore, I suggest moving to using the metadata field instead.

Working example:

from gridfs import GridFS
from flask import Flask
from flask_pymongo import PyMongo

app = Flask("app")
app.config['MONGO_URI'] = 'mongodb://localhost:27017/test'
mongo = PyMongo(app)
fs = GridFS(mongo.db)

filename = "..." # PNG image

fs.put(open(filename, 'rb'), filename=filename)

response = mongo.send_file(filename)

print(response.mimetype)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions