一、上传文件存储目录
在静态目录下创建名为upload的目录作为上传文件存储目录
二、原生文件上传
配置
代码语言:javascript复制BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">allowed_file</span><span class="hljs-params">(filename)</span>:</span>
<span class="hljs-keyword">return</span> <span class="hljs-string">"."</span> <span class="hljs-keyword">in</span> filename <span class="hljs-keyword">and</span> filename.rsplit(<span class="hljs-string">"."</span>, <span class="hljs-number">1</span>)[<span class="hljs-number">1</span>] <span class="hljs-keyword">in</span> DefaultConfig.ALLOWED_EXTENSIONS
<span class="hljs-comment"># 原生<a href="https://www.xgss.net/tag/文件上传" title="查看“文件上传”所有文章" target="_blank">文件上传</a></span>
<span class="hljs-comment"># 上传路径</span>
UPLOAD_FOLDER = os.path.join(BASE_DIR, <span class="hljs-string">"static/media"</span>)
<span class="hljs-comment"># 配置上传文件的最大尺寸,默认不受限制</span>
MAX_CONTENT_LENGTH = <span class="hljs-number">1024</span> * <span class="hljs-number">1024</span> * <span class="hljs-number">2</span>
<span class="hljs-comment"># 设置允许上传文件的类型</span>
ALLOWED_EXTENSIONS = [<span class="hljs-string">"jpg"</span>,<span class="hljs-string">"png"</span>,<span class="hljs-string">"jpeg"</span>,<span class="hljs-string">"gif"</span>]
<span class="hljs-comment"># 验证是否允许上传,允许返回True,否则返回False</span>
IS_ALLOWED_FUNC = allowed_file
上传界面
upfile.html
代码语言:javascript复制<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>上传文件</title>
</head>
<body>
<form action="/upfile/" method="post" enctype="multipart/form-data">
<input type="file" name="file"><br/>
<input type="submit" value="上传">
</form>
</body>
</html>
request.files
代码语言:javascript复制ImmutableMultiDict([(<span class="hljs-string">'file'</span>, <FileStorage: <span class="hljs-string">'a.txt'</span> (<span class="hljs-string">'text/plain'</span>)>)])
视图
代码语言:javascript复制<span class="hljs-comment"># 原生<a href="https://www.xgss.net/tag/文件上传" title="查看“文件上传”所有文章" target="_blank">文件上传</a>视图</span>
<span class="hljs-meta">@main.route('/upload/', methods=['GET', 'POST'])</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">upload</span><span class="hljs-params">()</span>:</span>
<span class="hljs-keyword">if</span> request.method == <span class="hljs-string">'GET'</span>:
<span class="hljs-keyword">return</span> render_template(<span class="hljs-string">'upload.html'</span>)
<span class="hljs-keyword">else</span>:
<span class="hljs-comment"># 获取上传文件对象</span>
file = request.files.get(<span class="hljs-string">'file'</span>)
filename = file.filename <span class="hljs-comment"># 获取文件名称</span>
<span class="hljs-keyword">if</span> <span class="hljs-keyword">not</span> current_app.config[<span class="hljs-string">'IS_ALLOWED_FUNC'</span>](filename):
<span class="hljs-keyword">return</span> <span class="hljs-string">'该文件类型不允许上传'</span>
filePath = os.path.join(current_app.config[<span class="hljs-string">'UPLOAD_FOLDER'</span>], filename)
file.save(filePath)
<span class="hljs-keyword">return</span> <span class="hljs-string">'<a href="https://www.xgss.net/tag/文件上传" title="查看“文件上传”所有文章" target="_blank">文件上传</a>成功'</span>
三、使用flask-uploads三方插件
安装
pip install flask-uploads
创建
代码语言:javascript复制<span class="hljs-keyword">from</span> flask_uploads <span class="hljs-keyword">import</span> UploadSet
<span class="hljs-keyword">from</span> flask_uploads <span class="hljs-keyword">import</span> TEXT, IMAGES
<span class="hljs-comment"># 与配置文件要对应 限制上传类型</span>
uploads = UploadSet(<span class="hljs-string">"UPLOADS"</span>, extensions=IMAGES)
<span class="hljs-keyword">from</span> .ext_uploads <span class="hljs-keyword">import</span> uploads
加载
代码语言:javascript复制<span class="hljs-keyword">from</span> exts <span class="hljs-keyword">import</span> uploads
<span class="hljs-keyword">from</span> flask_uploads <span class="hljs-keyword">import</span> configure_uploads
<span class="hljs-keyword">from</span> flask_uploads <span class="hljs-keyword">import</span> patch_request_class
<span class="hljs-comment">#加载上传三方对象</span>
configure_uploads(app, uploads)
<span class="hljs-comment"># 配置上传文件的大小,默认64M,如果size设置为None,会读取MAX_CONTENT_LENGTH配置的值</span>
patch_request_class(app, size=<span class="hljs-keyword">None</span>)
配置
代码语言:javascript复制<span class="hljs-comment">#上传文件</span>
UPLOADED_UPLOADS_DEST = os.path.join(BASE_DIR, <span class="hljs-string">"static/media"</span>)
MAX_CONTENT_LENGTH = <span class="hljs-number">2</span> * <span class="hljs-number">1024</span> * <span class="hljs-number">1024</span>
<span class="hljs-comment"># UPLOADED_UPLOADS_URL = "http://127.0.0.1:5000:8000/static/media/"</span>
示例
代码语言:javascript复制<span class="hljs-keyword">from</span> exts <span class="hljs-keyword">import</span> uploads
<span class="hljs-keyword">from</span> flask_uploads <span class="hljs-keyword">import</span> UploadNotAllowed
<span class="hljs-meta">@myApp.route("/upfile/", methods=["GET", "POST"])</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">upfile</span><span class="hljs-params">()</span>:</span>
<span class="hljs-keyword">if</span> request.method == <span class="hljs-string">"GET"</span>:
<span class="hljs-keyword">return</span> render_template(<span class="hljs-string">"upfile.html"</span>)
<span class="hljs-keyword">else</span>:
file = request.files.get(<span class="hljs-string">'file'</span>)
<span class="hljs-comment"># 获取保存文件的url</span>
img_url = uploads.url(file.filename)
print(img_url)
<span class="hljs-keyword">try</span>:
uploads.save(file, name=file.filename)
<span class="hljs-keyword">except</span> UploadNotAllowed <span class="hljs-keyword">as</span> e:
<span class="hljs-keyword">pass</span>
<span class="hljs-keyword">return</span> <span class="hljs-string">"上传文件成功"</span>