5

I'm using Flask Uploads for an upload form in my Flask application. However, whenever I try to save a file I get this error:

File "/Users/Documents/virtual_environment/bin/../lib/python2.7/os.py", line 157, in makedirs
    mkdir(name, mode)
OSError: [Errno 13] Permission denied: '/app'

It seems like uploads doesn't have the necessary permissions to save files? Here's the configuration I'm using for flask-uploads:

UPLOADS_DEFAULT_URL = os.environ.get("UPLOADS_URL", "http://localhost:5000/")
UPLOADS_DEFAULT_DEST = "/app/uploads/"
UPLOAD_EXTENSIONS = set(["csv", "xls", "xsls"])

Also, here's how I save the actual file:

@app.route('/upload', methods = ['GET', 'POST'])
@app.route('/upload/', methods = ['GET', 'POST'])
@roles_accepted('admin', 'team')
def r_upload():
    form = FileUploadForm()

    if form.validate_on_submit():
        filename = uploadSet.save(form.uploadfile.data)
        url = uploadSet.url(filename)
        flash("%s uploaded <a href=\'%s\'>HERE</a>!" % (filename, url))

    return render_template('/uploads.html',
        dashboard_title = "%s Uploads" % g.name,
        form = form)

The error is caused by the save line. Any suggestions on how to fix this? Thanks.

Jeff Widman
  • 22,014
  • 12
  • 72
  • 88
kevin.w.johnson
  • 1,684
  • 3
  • 18
  • 37

3 Answers3

4

Does your linux user have file permissions on /app/uploads/? Check that using ls -la /app/uploads.

Note that /app would try to write files in the root of the filesystem at /.

If you are looking to write files within your app, use app/uploads instead of /app/uploads/. From the error, it looks like /app simply does not exist and writing to app/uploads is precisely what you wanted to do.

Anshul Goyal
  • 73,278
  • 37
  • 149
  • 186
  • Was just going to post this answer myself! Just figured it out. Thanks! That is strange, it seems the flask-uploads documentation has the directory set the other way. – kevin.w.johnson Feb 12 '15 at 16:19
  • 1
    In general, using absolute paths on the file system leads to fewer surprises. You can use `os.path.abspath(os.path.dirname(__file__))` to make sure you're always working relative to the location of the current module. At the end of the day, you can do it however you want, just make sure the user running the WSGI application as write permissions to the folder. – dirn Feb 12 '15 at 18:13
0

I know this is 5 years late, but I'll put this here for people who see this in the future.

My solution was to create an empty file with the same name (open("path/to/file.png").close()) and then save the file

0

If you are also facing problem of (PermissionError: [Errno 13] Permission denied:) so may this is helpful for you.

#imports from flask import Flask, render_template, reques

#code [python] @app.route("/img", methods=["GET", "POST"]) def get_img(): r = request.files.get("file1") with open("n.png", "wb") as fp: for itm in r: fp.write(itm) return "done"

code [html]

<html>
<head>

</head>

<body>
<form action="/img" method="post" enctype="multipart/form-data">
    <input type="file" id="22" style="margin-top: 100px;" name="file1">
    <button type="submit" style="margin-top: 100px;">send</button>
</form>
</body>
</html>
R242
  • 1
  • 1