Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Upload image in Flask

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

like image 513
Teor9300 Avatar asked Jul 05 '17 12:07

Teor9300


People also ask

How do I upload pictures to flask?

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.

How do I show an image in a flask?

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.


3 Answers

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

like image 192
PYA Avatar answered Oct 17 '22 20:10

PYA


  1. your form's enctype should be multipart/form-data (otherwise it does not work):
<form method="post" enctype="multipart/form-data">
...
</form>
  1. You should specify upload folder (otherwise it does not work):

app.config['UPLOAD_FOLDER'] = '/path/to/the/uploads'

  1. You should specify a name for your file input (otherwise it does not work):
<input type="file" name="file1">
  1. you should manually save the file with file save() method:
path = os.path.join(app.config['UPLOAD_FOLDER'], file1.filename)
file1.save(path)
  1. Simple full example:
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()
  1. Detailed full example: https://flask.palletsprojects.com/en/1.1.x/patterns/fileuploads/
like image 14
yaya Avatar answered Oct 17 '22 22:10

yaya


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)))
like image 3
Meenu Avatar answered Oct 17 '22 21:10

Meenu