如何将pdf文件和图像文件上传到flask的不同上传文件夹中?

时间:2019-06-17 07:00:43

标签: python flask file-upload web-deployment

我希望有人可以调整我的代码,以便它可以一次在同一页面上载pdf和图像文件。 以下是html部分

<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>

下一个是烧瓶代码。我还没有上传 pdf文件,导致以下使用图片文件

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')

我尝试将所有内容加倍以查看是否可行。

没有用。任何帮助将不胜感激

1 个答案:

答案 0 :(得分:0)

您的HTML表单应具有在其中发布数据的操作。

<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>

您的FLASK代码几乎相同,您只需要检查文件名,并根据文件名即可完成工作。

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')