I have to upload some images in static folder of my project directory, but i don't know how to say it to my code. In the follow code.py i'm able to upload an image and store it in the project directory at the same level of static folder, but i would that this image can be store INSIDE static folder.
@app.route('/uploader', methods = ['GET', 'POST'])
def upload_file():
if request.method == 'POST':
f = request.files['file']
f.save(secure_filename(f.filename))
return render_template('end.html')
What i have to do?? Thanks guys
Handling file upload in Flask is very easy. It needs an HTML form with its enctype attribute set to 'multipart/form-data', posting the file to a URL. The URL handler fetches file from request. files[] object and saves it to the desired location.
To display image on a HTML page with Python Flask, we can pass the image path to the template from the view. Then we call render_template with the template file name, and the user_image argument set to the image path. to interpolate the user_image in the template.
you need to define the upload folder
from the flask
documentation
import os
from flask import Flask, flash, request, redirect, url_for
from werkzeug.utils import secure_filename
UPLOAD_FOLDER = '/path/to/the/uploads'
ALLOWED_EXTENSIONS = set(['txt', 'pdf', 'png', 'jpg', 'jpeg', 'gif'])
app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
def upload_file():
if request.method == 'POST':
# check if the post request has the file part
if 'file' not in request.files:
flash('No file part')
return redirect(request.url)
file = request.files['file']
# if user does not select file, browser also
# submit a empty part without filename
if file.filename == '':
flash('No selected file')
return redirect(request.url)
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
https://flask.palletsprojects.com/en/1.1.x/patterns/fileuploads/
So your code would be UPLOAD_FOLDER = '/path/to/static/images'
or something like that
enctype
should be multipart/form-data
(otherwise it does not work):<form method="post" enctype="multipart/form-data">
...
</form>
app.config['UPLOAD_FOLDER'] = '/path/to/the/uploads'
<input type="file" name="file1">
save()
method:path = os.path.join(app.config['UPLOAD_FOLDER'], file1.filename)
file1.save(path)
import os
from flask import Flask, request
UPLOAD_FOLDER = './upload'
app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
@app.route('/', methods=['GET', 'POST'])
def upload_file():
if request.method == 'POST':
if 'file1' not in request.files:
return 'there is no file1 in form!'
file1 = request.files['file1']
path = os.path.join(app.config['UPLOAD_FOLDER'], file1.filename)
file1.save(path)
return path
return 'ok'
return '''
<h1>Upload new File</h1>
<form method="post" enctype="multipart/form-data">
<input type="file" name="file1">
<input type="submit">
</form>
'''
if __name__ == '__main__':
app.run()
Add enctype="multipart/form-data"
to form that contains your input type=file
After that,
from os.path import join, dirname, realpath
from werkzeug.utils import secure_filename
UPLOADS_PATH = join(dirname(realpath(__file__)), 'static\\img')
if request.files['image'].filename != '':
image = request.files['image']
image.save(os.path.join(UPLOADS_PATH, secure_filename(image.filename)))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With