<form method="post" enctype="multipart/form-data" class="dropzone" action="import">
初始化:
from flask_dropzone import Dropzone
app = Flask( name )
dropzone = Dropzone(app)
from flask_dropzone import Dropzone
dropzone = Dropzone()
def create_app():
app = Flask( name )
dropzone.init_app(app)
return app
<head>
...
{{ dropzone.load_css() }}
</head>
<body>
...
{{ dropzone.load_js() }}
</body>
<head>
{{ dropzone.style('border: 2px dashed #0087F7margin: 10%') }}
</head>
{{ dropzone.create(action='处理上传文件的路由URL') }}
import os
from flask import Flask, request
from flask_dropzone import Dropzone
app = Flask( name )
dropzone = Dropzone(app)
@app.route('/uploads', methods=['GET', 'POST'])
def upload():
DROPZONE_REDIRECT_VIEW
@app.route('/', methods=['POST', 'GET'])
def upload():
if request.method == 'POST':
f = request.files.get('file')
if f.filename.split('.')[1] != 'png':
return 'PNG only!', 400 # return the error message, with a proper 4XX code
f.save(os.path.join('the/path/to/save', f.filename))
return render_template('index.html')
DROPZONE_SERVE_LOCAL 是否使用内置的本地资源
DROPZONE_MAX_FILE_SIZE 允许文件最大值,单位MB
DROPZONE_INPUT_NAME 上传字段
DROPZONE_ALLOWED_FILE_CUSTOM False是否使用自定义文件类型允许规则
DROPZONE_ALLOWED_FILE_TYPE 'default' 允许的文件类型
DROPZONE_MAX_FILES ‘null’ 一次可以上传的文件数量最大值
DROPZONE_DEFAULT_MESSAGE“Drop files here to upload” 上传区域显示的提示文字
DROPZONE_INVALID_FILE_TYPE “You can’t upload files of this type.” 文件类型错误的错误消息
DROPZONE_FILE_TOO_BIG “File is too big {{filesize}}. Max filesize: {{maxFilesize}}MiB.” 文件太大时显示的错误消息
DROPZONE_SERVER_ERROR “Server error: {{statusCode}}” 服务器错误的错误消息
DROPZONE_BROWSER_UNSUPPORTED“Your browser does not support drag’n’drop file uploads.” 浏览器不支持的错误消息
DROPZONE_MAX_FILE_EXCEED“Your can’t upload any more files.” 超过最大文件数量限制的错误消息
ROPZONE_UPLOAD_MULTIPLE False 是否在单个请求中发送多个文件,默认一个请求发送一个文件
DROPZONE_PARALLEL_UPLOADS 2 当DROPZONE_UPLOAD_MULTIPLE设为True时,设置单个请求包含的文件数量
DROPZONE_REDIRECT_VIEW None上传完成后重定向的模板端点
DROPZONE_ENABLE_CSRF False 是否开启CSRF保护
default 所有
image 图片
audio 音频
video
text 文本
app程序
app.config['DROPZONE_ALLOWED_FILE_TYPE'] = 'image'
app.config['DROPZONE_ALLOWED_FILE_CUSTOM'] = True
app.config['DROPZONE_ALLOWED_FILE_TYPE'] = 'image/*, .pdf, .txt'