PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, July 15, 2022

[FIXED] How to upload a pdf file and an image file to different upload folders in flask?

 July 15, 2022     file-upload, flask, python, web-deployment     No comments   

Issue

I was hoping someone could adjust my code so that it could upload a pdf and image file at once on the same page. Below is the html part

<form method="post" enctype="multipart/form-data">
    Profile Image:
     <br>
    <input type="file" name="img-file" class="bg-warning" style="padding:10px" required>
    <br><br>

    Pdf:
    <br>
    <input type="file" name="pdf-file" class="bg-warning" style="padding:10px" required>

    <h6 class="text-success">{{ msg_name }}</h6>
    <br><br>
    <input type="submit" name="submit" value="UPLOAD" class="btn btn-outline-warning">
</form>

next is the flask code. I havent included the upload of pdf file cause the below worked with image files

UPLOAD_FOLDER = 'C:\\Users\\...\\static\\image\\'
ALLOWED_EXTENSIONS = set(['png', 'jpg', 'jpeg', 'gif'])
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER


def allowed_file(filename):
    return '.' in filename and \
           filename.rsplit('.', 1)[1] in ALLOWED_EXTENSIONS

@app.route('/imageUpload', methods=['GET', 'POST'])
def upload_file():
    if request.method == 'POST':
        # check if the post request has the file part
        if 'img-file' not in request.files:
            flash('No file part')
            return redirect(request.url)
        file = request.files['img-file']
        # if user does not select file, browser also
        # submit an 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))
            return redirect(url_for('uploads'))

    return render_template('upload-image.html')

I tried doubling everything to see if that would work.

didn't work. Any help will be appreciated


Solution

Your HTML form should have an action where to post the data.

<form method="post" enctype="multipart/form-data" action="localhost:3000/up">
    Profile Image:
     <br>
    <input type="file" name="img-file" class="bg-warning" style="padding:10px" required>
    <br><br>

    Pdf:
    <br>
    <input type="file" name="pdf-file" class="bg-warning" style="padding:10px" required>

    <h6 class="text-success">{{ msg_name }}</h6>
    <br><br>
    <input type="submit" name="submit" value="UPLOAD" class="btn btn-outline-warning">
</form>

Your FLASK code would be pretty much the same, you just have to check for the file-names and based on file-names you can do your work.

UPLOAD_FOLDER = 'C:\\Users\\...\\static\\'
ALLOWED_EXTENSIONS = set(['png', 'jpg', 'jpeg', 'gif'])
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER

def allowed_file(filename):
    return '.' in filename and \
           filename.rsplit('.', 1)[1] in ALLOWED_EXTENSIONS

@app.route('/upload', methods=['GET', 'POST'])
def upload_file():
    if request.method == 'POST':
        # check if the post request has the file part
        if 'img-file' not in request.files:
            flash('No img file')
            return redirect(request.url)
        if 'pdf-file' not in request.files:
            flash('No pdf file')
            return redirect(request.url)
        img_file = request.files['img-file']
        pdf_file = request.files['pdf-file']
        # if user does not select file, browser also
        # submit an empty part without filename
        if img_file.filename == '':
            flash('No image file selected')
            return redirect(request.url)
        if pdf_file.filename == '':
            flash('No pdf file selected')
            return redirect(request.url)
        if img_file and allowed_file(img_file.filename):
            filename = secure_filename(img_file.filename)
            file.save(os.path.join(app.config['UPLOAD_FOLDER'] + "\image\" , filename))
        if pdf_file and allowed_file(pdf_file.filename):
            filename = secure_filename(pdf_file.filename)
            file.save(os.path.join(app.config['UPLOAD_FOLDER'] + "\pdf\" , filename))
        return redirect(url_for('uploads'))
    return render_template('upload-image.html')


Answered By - Tech at The Sparks Foundation
Answer Checked By - Pedro (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing