Reputation: 71
I want to manage to upload and save files into ./project/client/static but whenever I tried to save the file it always end up at ./project/server/.
I want to configure the default upload folder to be ./project/client/static or a temporary folder.
# project/server/main/views.py
@main_blueprint.route("/tasks", methods=["POST"])
def run_task():
file = request.files.get('file')
if file.filename == '':
flash('No selected file')
return redirect(request.url)
print('downloading', file.filename)
filename = file.filename
file = request.files.get('file')
filepath = filename
print('downloading file', filename)
file.save(filepath)
print('download complete')
print('starting task to predict file')
info = { 'file': filepath }
task = create_task.delay(info)
print(task.id)
return jsonify({"task_id": task.id}), 200
This always lead the downloaded file to be saved to ./project/server
I am also using flask Blueprint
# project/server/main/views.py
import os
from flask import render_template, Blueprint, jsonify, request, Response, send_file, redirect, url_for
from celery.result import AsyncResult
from project.server.tasks import create_task
from project.machine_learning import app as machine_learning
main_blueprint = Blueprint("main", __name__, static_folder='static')
upload_folder = './project/client/static/'
file structure
| - root
| - readme_files
| - project
| | - machine_learning
| | | - labelled_comments
| | | - models
| | | - notebooks
| | | - src
| | | | - text_preprocessing
| | | | - csv_file_modifier
| | | | | - pyunittest
| | | | - keyword_filter
| | | | | - pyunittest
| | | | | | - keyword-dictionaries
| | | | | - keyword-dictionaries
| | - tests
| | - server
| | | - main
| | - client
| | | - static
| | | | - archive
| | | - templates
Upvotes: 5
Views: 1478
Reputation: 3517
You can specify that the file should be saved in upload_folder
. The approach I would take is to add an environment variable that defines the path to the upload folder.
# .env file
UPLOAD_PATH=project/client/static/
Then, set up a configuration to access UPLOAD_PATH
:
# config.py
import os
class Config(object):
UPLOAD_PATH = os.environ.get("UPLOAD_PATH")
The extension python-dotenv
will be useful here, so install it. Load this configuration in your application's instance(where you have defined app
):
# __init__.py
from config import Config
app = Flask(__name__)
app.config.from_object(Config)
Update the save()
function in your routes as follows:
import os
@main_blueprint.route("/tasks", methods=["POST"])
def run_task():
# ...
file.save(os.path.join(app.config["UPLOAD_PATH"], filepath)
# ...
Upvotes: 3