0

I am developing a web app with Flask using CS50 IDE. I want the user to upload a file with a form. But when the form is submitted, I get a "500 Internal Server Error", whether a file is selected on not.

If a file is selected, there are no error logs in the console.

If NO file is selected, there is the following error log:

ERROR:application:Exception on /new-item [POST]
Traceback (most recent call last):
  File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 2446, in wsgi_app
    response = self.full_dispatch_request()
  File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 1951, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 1820, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/usr/local/lib/python3.7/site-packages/flask/_compat.py", line 39, in reraise
    raise value
  File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 1949, in full_dispatch_request
    rv = self.dispatch_request()
  File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 1935, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/home/ubuntu/final-project/helpers.py", line 34, in decorated_function
    return f(*args, **kwargs)
  File "/home/ubuntu/final-project/application.py", line 45, in decorated_function
    return f(*args, **kwargs)
  File "/home/ubuntu/final-project/application.py", line 350, in newItem
    file.save(os.path.join(app.config['IMAGE_UPLOADS'], filename))
  File "/usr/local/lib/python3.7/site-packages/werkzeug/datastructures.py", line 2800, in save
    dst = open(dst, "wb")
IsADirectoryError: [Errno 21] Is a directory: 'static/img/menu/'

Flask code

app.config["IMAGE_UPLOADS"] = "static/img/menu/"

@app.route("/new-item", methods=['GET', 'POST'])
def newItem():
    if request.method == "POST":

        if request.files:
            file = request.files["file"]
            filename = secure_filename(file.filename)
            file.save(os.path.join(app.config['IMAGE_UPLOADS'], filename))

HTML code

<form action="/new-item" method="post" enctype="multipart/form-data">
    <div>
        <input type="file" name="file">
        <label>Choose Image</label>
    </div>
    <button type="submit">Submit</button>
</form>

Any idea what's causing the error?

1 Answers1

0

You need to remove the trailing slash, this is a misuse of os.path.join

app.config["IMAGE_UPLOADS"] = "static/img/menu" # drop the trailing slash

See Why doesn't os.path.join() work in this case?

You may also need to update app.config["IMAGE_UPLOADS"] to be an absolute path instead of a relative path.

pistolpete
  • 968
  • 10
  • 20
  • I have dropped the trailing slash and replaced the path with "os.getcwd()+'/static/img/menu'". Traceback: IsADirectoryError: [Errno 21] Is a directory: '/home/ubuntu/final-project/static/img/menu/' – user14004864 Jul 27 '20 at 19:08
  • Ok that must be the error for when no file is selected. You need to add handling for when no file is selected such as `if not filename: return # do nothing and keep the user on the same page` or display an error message to the user. What happens when you select a file? – pistolpete Jul 27 '20 at 19:13
  • When a file is selected, I do not get any traceback in the console. I do get a 500 response in my browser from nginx though. FYI, I'm running Flask within the "final-project" directory. – user14004864 Jul 27 '20 at 19:20
  • Is the file actually uploaded? Is it in your directory? I think nginx is irrelevant to this question – pistolpete Jul 27 '20 at 19:22
  • No, the file is not uploaded and is not present in the directory. – user14004864 Jul 27 '20 at 19:24
  • Hmm, can you add these print statements and tell me the output? `print(file)`, `print(file.filename)`, `print(filename)` – pistolpete Jul 27 '20 at 19:27
  • No logs when a file is selected. `` when no file selected. – user14004864 Jul 27 '20 at 19:33