Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stores Files as 0 byte files #98

Open
lcundiff opened this issue Sep 20, 2020 · 1 comment
Open

Stores Files as 0 byte files #98

lcundiff opened this issue Sep 20, 2020 · 1 comment
Assignees
Labels
question Further information is requested

Comments

@lcundiff
Copy link

lcundiff commented Sep 20, 2020

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 joegasewicz self-assigned this Sep 22, 2020
@joegasewicz
Copy link
Owner

joegasewicz commented Sep 22, 2020

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

@joegasewicz joegasewicz added the question Further information is requested label Sep 22, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants