我正在Python中构建rest api。
我正在尝试在 python 中上传视频文件并获取它的缩略图。
但当文件上传时,文件显示大小为零 kb。
下面是我的代码的快照:
import json
from flask import Flask, make_response, request, abort, logging, Response
import os
from werkzeug.utils import secure_filename
#from flask.ext.uploads import delete, init, save, Upload
app = Flask("main")
app.config['ALLOWED_EXTENSIONS'] = set(['mp4','jpg'])
app.config['UPLOAD_FOLDER'] = 'uploads/'
@app.route('/import', methods=['POST'])
def import_objects():
file = request.files['file']
# logging.debug('uploading file ' + file.filename)
if file and allowed_file(file.filename):
# extract content
content = file.read()
filename = secure_filename(file.filename)
print(filename)
print(os.path.join(app.config['UPLOAD_FOLDER'], filename))
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
#save(request.files['file'])
#print content
print('ffmpeg -i ./'+os.path.join(app.config['UPLOAD_FOLDER'], filename)+' -ss 00:00:10 -vframes 1 -f image2 ' + os.path.join(app.config['UPLOAD_FOLDER'],'thumb1.jpg'))
os.system('ffmpeg -i ./'+os.path.join(app.config['UPLOAD_FOLDER'], filename)+' -ss 00:00:10 -vframes 1 -f image2 ' + os.path.join(app.config['UPLOAD_FOLDER'],'thumb1.jpg'))
jsonResponse = json.dumps({'file_content': content})
response = Response(jsonResponse, mimetype='application/json')
return "sucess"
else:
abort(make_response("File extension not acceptable", 400))
def allowed_file(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1] in app.config['ALLOWED_EXTENSIONS']
app.run("0.0.0.0", 8080, True)
hear 是我的 html 文件的代码片段。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<form method="post" action="http://0.0.0.0:8080/import" enctype="multipart/form-data">
<input type="file" name="file"/>
<input type="submit">
</form>
</body>
</html>
最佳答案
我怀疑你的content = file.read()
搞砸了。删除该行,它应该可以工作。保存文件之前不需要读取该文件。保存即可。
关于python - 在 python Flask 中上传后文件显示零 kb,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32396697/