joegasewicz / flask-file-upload

Easy file uploads for Flask.
MIT License
154 stars 15 forks source link

Stores Files as 0 byte files #98

Open lcundiff opened 4 years ago

lcundiff commented 4 years ago

Not sure if I am not using file_upload.save_files correctly, but the files that it saves are 0 bytes but have the correct name.

Here is how I am using save_files:

userFileModel = UserFiles(userId=current_user.id) userFile = file_upload.save_files(userFileModel, files={ "userFile": file, })

And here is my UserFiles model:

@file_upload.Model class UserFiles(db.Model): tablename = "UserFiles" id = db.Column(db.Integer, primary_key=True) userFile = file_upload.Column() userId = db.Column(db.Integer, db.ForeignKey("User.id"))

Here is my upload folder configuration: app = Flask(name) app.config["ALLOWED_EXTENSIONS"] = ["jpg", "png", "mov", "mp4", "mpg"] app.config["MAX_CONTENT_LENGTH"] = 1000 1024 1024 # 1000mb app.config["UPLOAD_FOLDER"] = os.path.join(basedir,"./static/uploads") db = SQLAlchemy(app) file_upload = FileUpload(app, db)

Not sure if this is a bug or an error on my end, but I have read through the documentation and it seems like this should yield files stored on static/uploads with their full data, but instead they are empty.

joegasewicz commented 4 years ago

Hi @lcundiff

I have just run the below code successfully with the latest version of FlaskFileUpload - 0.1.4.

from flask import Flask, request
from os.path import join, dirname, realpath
from flask_sqlalchemy import SQLAlchemy
from flask_file_upload import FileUpload

app = Flask(__name__, static_folder="static")
db = SQLAlchemy(app)

app.config["UPLOAD_FOLDER"] = join(dirname(realpath(__file__)), "static/uploads")

# Other FLASK config varaibles ...
app.config["ALLOWED_EXTENSIONS"] = ["jpg", "png", "mov", "mp4", "mpg"]
app.config["MAX_CONTENT_LENGTH"] = 1000 * 1024 * 1024  # 1000mb
app.config["SQLALCHEMY_DATABASE_URI"] = "postgresql://localhost:5432/flask_file_upload"

file_upload = FileUpload(
    app,
    db,
)

@file_upload.Model
class UserFiles(db.Model):
    __tablename__ = "UserFiles"
    id = db.Column(db.Integer, primary_key=True)
    userFile = file_upload.Column()
    userId = db.Column(db.Integer)

@app.route("/files", methods=["POST"])
def files():
    file = request.files["placeholder_img"]
    userFileModel = UserFiles(userId=1)
    userFile = file_upload.save_files(userFileModel, files={
        "userFile": file,
    })

    db.session.add(userFile)
    db.session.commit()

    return {
        "data": "placeholder_img saved",
    }, 200

if __name__ == "__main__":
    db.create_all()
    app.run(port=5000)

This saves the file of (in my case ) 3.4 MB in the UPLOAD_FOLDER directory path:

└── static
    └── uploads
        └── UserFiles
            └── 1
                └── pic.png

Can you please run this script on your system & check to see if this works, if it doesn't please can you post back your system's OS name & version you're running, thanks. Joe