Flask上载PDF文件而不移至目标文件夹

时间:2018-07-09 11:30:14

标签: python flask

我从https://gist.github.com/liulixiang1988/cc3093b2d8cced6dcf38获得了代码,并尝试进行一些修改;这样我就可以在我的flask Web应用程序中使用它。这不是将pdf文件上传到目标文件夹。上传后,我想显示以其他html格式上传的文件列表。

upload.html

<!DOCTYPE html>
<html lang="en">
<head>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
</head>
<body>
    <div class="container">
        <div class="header">
            <nav>
                <ul class="nav nav-pills pull-right">
                    <li role="presentation" class="active">
                        <a href="/logout">Logout</a>
                    </li>
                </ul>
            </nav>
            <h3 class="text-muted">Python Flask App</h3>
        </div>
        </div>
    <div class="container">
        <div class="jumbotron">
            <div class="container">
                <div class="header">
                    <h3 class="text-muted">Uploaded files</h3>
                </div>
                <hr />
                <div>
                    This is a list of the files you just uploaded, click on them to load/download them
                    <ul>
                        {% for file in filenames %}
                        <li><a href="{{url_for('uploaded_file', filename=file)}}">{{file}}</a></li>
                        {% endfor %}
                    </ul>
                </div>
                <div class="header">
                    <h3 class="text-muted">Code to manage a Upload</h3>
                </div>
            </div>
        </div>
    </div>
</body>
</html>

Uploadpage.html

<!DOCTYPE html>
<html lang="en">
<head>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
    <link href="../static/signup.css" rel="stylesheet">
    <script src="../static/js/jquery-1.11.2.js"></script>
    <script src="../static/js/signUp.js"></script>
    <script src="../static/js/dataup.js"></script>
</head>
<body>
    <div class="container">
        <div class="header">
            <nav>
                <ul class="nav nav-pills pull-right">
                    <li role="presentation" class="active">
                        <a href="/logout">Logout</a>
                    </li>
                </ul>
            </nav>
            <h3 class="text-muted">Python Flask App</h3>
            <br />
        </div>
    </div>
    <div class="container">
        <div class="topnav">
            <a class="active" href="#home">Home</a>
            <a href="#news">News</a>
            <a href="#contact">Contact</a>
            <a href="#about">About</a>
        </div>
    </div>
    <div class="container">
        <div class="jumbotron">
            <div class="container">
                <div class="header">
                    <h3 class="text-muted">How To Upload a File</h3>
                </div>
                <hr />
                <div>
                    <form method="POST" action="{{ url_for('upload') }}" enctype="multipart/form-data">
                        <input type="file" multiple="" name="file[]" class="span3" /><br />
                        <input type="submit" value="Upload" class="span2">
                    </form>
                </div>
            </div>
        </div>
    </div>
</body>
</html>

from flask import Flask, flash, render_template, json, request, send_from_directory
from flaskext.mysql import MySQL
from werkzeug import generate_password_hash, check_password_hash, secure_filename
from flask import session
from flask import redirect
import os


UPLOAD_FOLDER = 'C:/Users/pavan/Desktop/uploads/'
ALLOWED_EXTENSIONS = set(['txt', 'pdf', 'png', 'jpg', 'jpeg', 'gif'])

app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
app.config['ALLOWED_EXTENSIONS'] = ALLOWED_EXTENSIONS

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

@app.route('/')
def index():
    return render_template('uploadpage.html')


@app.route('/upload', methods=['POST'])
def upload():
    uploaded_files = request.files.getlist("file[]")
    filenames = []
    for file in uploaded_files:
    if file and allowed_file(file.filename):
        filename = secure_filename(file.filename)
        file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
        filenames.append(filename)
    return render_template('upload.html', filenames=filenames)

@app.route('/uploads/<filename>')
def uploaded_file(filename):
    return send_from_directory(app.config['UPLOAD_FOLDER'],
                           filename)

0 个答案:

没有答案