Как загрузить PDF-файл и файл изображения в различные папки загрузки в колбе? - PullRequest
0 голосов
/ 17 июня 2019

Я надеялся, что кто-нибудь сможет настроить мой код так, чтобы он мог одновременно загружать файлы 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 голосов
/ 17 июня 2019

Ваша 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')
...