python利用flask实现文件上传并保存

#!/usr/bin/python3
#encoding:utf-8
#来自官网,但官网的不能直接用,稍微修改了一下,秒秒钟运行成功,哈哈哈哈
import os
from flask import Flask, request, redirect, url_for
from werkzeug.utils import secure_filename
from flask import send_from_directory
UPLOAD_FOLDER = './static/uploads'#上传到这里
ALLOWED_EXTENSIONS = set(['txt', 'pdf', 'png', 'jpg', 'jpeg', 'gif','jpeg','rar','doc','docx','dot','pptx','db'])#允许的格式,保证安全性
app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
app.config['MAX_CONTENT_LENGTH'] = 64 * 1024 * 1024#限制大小64mb
def allowed_file(filename):
    return '.' in filename and \
           filename.rsplit('.', 1)[1] in ALLOWED_EXTENSIONS
@app.route('/', methods=['GET', 'POST'])
def upload_file():
    if request.method == 'POST':
        file = request.files

你可能感兴趣的:(Python,flask,网站,python,服务器)