3

I have the following file structure:

.
├── app
│   ├── api_routes
│   │   ├── forms.py
│   │   ├── __init__.py
│   │   └── routes.py
│   ├── __init__.py
│   ├── main_routes
│   │   ├── forms.py
│   │   ├── __init__.py
│   │   └── routes.py
│   ├── models.py
│   ├── static
│   │   └── styles.css
│   ├── templates
│   │   └── base.html
│   └── uploads
│       └── 10_0_0.jpg
├── application.py
└── config.py

In my config.py I have this:

class Config(object):
    UPLOAD_FOLDER = 'uploads/'

When I'm saving an uploaded file, and then sending it back to the user (just as example) I'm using:

fname = 'foo.jpg'
fname_save = os.path.join(current_app.root_path, current_app.config['UPLOAD_FOLDER'], fname)
fname_retr = os.path.join(current_app.config['UPLOAD_FOLDER'], fname)
file.save(fname_save)
return send_from_directory(os.path.dirname(fname_retr),
                           os.path.basename(fname_retr))

Having different names for the upload folder in the cwd (where the file is saved), and the folder the flask module is running (app/), is a bit tedious. Are there any more elegant solutions than my current one to solve this issue?

Joost
  • 3,609
  • 2
  • 12
  • 29

1 Answers1

2

What I would do this :

@app.route('/upload', methods=['POST'])
def myroute():
    fname = 'foo.jpg'
    file = request.file[0] # not save at all
    send_back_file = io.BytesIO(file.read())
    file.seek(0)
    file.save(os.path.join(app.config['UPLOAD_FOLDER'], fname))
    return send_file(send_back_file, attachment_filename=fname, as_attachement=True)

Resource :

Rémi Desgrange
  • 868
  • 1
  • 6
  • 20