python - 在通过烧瓶中的http post请求上传之前压缩文件
问题描述
我正在制作的网络工具上传速度很慢,所以我想上传压缩文件。是否可以在 Flask 中上传文件(通过 http post 请求)之前对其进行压缩?
我想将 gzip 与以下内容一起使用,但不确定是否可以在上传之前进行压缩。
test = open(path_to_uploaded_file, mode="rb")
test_2 = test.read()
hi = gzip.compress(test_2)
new_file = open(session["file_path"].replace(".mgh", ".mgz"), "wb+")
new_file.write(hi)
这是我用于文件上传的框架(来源:https ://flask.palletsprojects.com/en/1.1.x/patterns/fileuploads/ )
import os
from flask import Flask, flash, request, redirect, url_for
from werkzeug.utils import secure_filename
UPLOAD_FOLDER = '/path/to/the/uploads'
ALLOWED_EXTENSIONS = {'txt', 'pdf', 'png', 'jpg', 'jpeg', 'gif'}
app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
def allowed_file(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
@app.route('/', methods=['GET', 'POST'])
def upload_file():
if request.method == 'POST':
# check if the post request has the file part
if 'file' not in request.files:
flash('No file part')
return redirect(request.url)
file = request.files['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('uploaded_file',
filename=filename))
return '''
<!doctype html>
<title>Upload new File</title>
<h1>Upload new File</h1>
<form method=post enctype=multipart/form-data>
<input type=file name=file>
<input type=submit value=Upload>
</form>
'''
谢谢!
解决方案
推荐阅读
- woocommerce - 达到免运费金额后如何设置免运费
- javascript - 如何将字符串添加到数组并返回字符串
- angular - Angular:无法使用@Input第二次重置子组件中的值
- typo3 - TYPO3 服务不可用 (503)
- c++ - hiredis 消息未到达订阅的处理程序
- node.js - 我想在 puppeteer-cluster 中创建无限循环
- python - 为什么 Elif 在一些列表输入后打印?
- elasticsearch - 带有前缀查询的弹性搜索不区分大小写的查询
- javascript - 如何将图像 URL 替换为 JSON 对象中的图像标记?
- c# - SendNotifyMessage 和 WM_SETTINGCHANGE 不再工作了?(视窗 10)