python - Flask mp4文件未使用文件路径显示在视频标签中
问题描述
我有一个烧瓶应用程序,它将 mp4 文件存储在本地目录中,并将路径保存在我的数据库中。我可以访问send_file
烧瓶提供的文件用户并下载,但是当我将路径传递给视频标签时,它没有被显示。请参见下面的代码:
保存视频文件:works fine as expected
@app.route('/', methods=['GET', 'POST'])
def index():
form = VideoPicForm()
if form.validate_on_submit():
if form.vid.name not in request.files:
flash('No video part specified')
return redirect(url_for('index'))
file = request.files[form.vid.name]
if file.filename == '':
flash('No file selected for uploading')
return redirect(url_for('index'))
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
filepath = os.path.join(app.root_path, app.config['UPLOAD_FOLDER'], filename)
file.save(filepath)
video= VideosFiles(description=form.description.data, vidpath=filepath)
db.session.add(video)
db.session.commit()
flash('video uploaded successfully...')
return redirect(url_for('index'))
return render_template('index.html', title='Home', form=form)
从模板访问:```文件未呈现`
@app.route('/display')
def display():
file_data= VideosFiles.query.filter_by(id=1).first()
video= file_data.vidpath
return render_template('display.html', title='Videos', video=video)
在模板中:
<video width="320" height="240" controls>
<source src="{{video}}" type="video/mp4" />
</video>
但是当我使用 下载时send_file
,视频会立即下载:
@app.route('/download')
def download():
file_data= VideosFiles.query.filter_by(id=1).first()
video= file_data.vidpath
return send_file(video, as_attachment=True)
我错过了导致视频未在模板中呈现的任何内容?
解决方案
您的代码有一个小的变化。确保您的视频存储在烧瓶应用程序结构的静态文件夹中。
我和你有同样的问题,当我将视频存储在静态文件夹中时,它工作正常。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<video width="400" controls>
<source src="{{ url_for('static', filename='small.mp4') }}" type="video/mp4">
</video>
</body>
</html>
也请使用 url_for 进行 html 渲染。
推荐阅读
- excel - 如何在 Power Query 的单元格中对每个出现的子字符串进行编号?
- linux - 如何跟踪重新调度中断
- javascript - 在 ReactJS 中映射数组时,浏览器中不显示任何内容,即使该数组具有元素
- postgresql - 如何在 Windows 10 上检查 postgreSQL 的用户名
- c++ - 实现递归函数
- python - 网页抓取/网页抓取
- javascript - 用于旋转图像的 Javascript 小书签无法正常工作
- java - 在 MongoRepository spring 中保存选定字段而不影响其他字段
- python - 相邻文本的记录数据
- c# - 查询 LINQ to Entities 中联系字段的属性