请问我错过了什么。当我想提供已下载到磁盘上的视频文件时,我不断在浏览器中收到内部服务器错误。 这是我的代码: View 函数
@view_config(name='download_video')
def dl(request):
url = request.params.get('url')
camefrom = request.params.get('came_from')
path_dir = request.registry.settings['app.download_path']
result= save_to_disk(url, path_dir)
if result:
filename = result['filename']
filepath = os.path.abspath(os.path.join(path_dir,filename))
file_exists = os.path.exists(filepath)
if file_exists:
res = FileResponse(filepath,content_type='video/mp4')
res.headers['Content-Length'] = os.path.getsize(filepath)
res.headers['Content-Disposition'] = 'attachment;filename=%s'%filename
return res
request.session.flash(_("Error downloading video, please check your network","danger"))
return HTTPFound(location=camefrom)
模板
<a tal:attributes="href api.url(api.root, '@@download_video', query={'came_from': request.url,'url':context.video_url})" class="btn">Download</a>
视频下载完毕,我可以在文件夹中看到它,但我不断收到内部服务器错误,而浏览器上没有回溯
我的应用程序依赖于 Kotti cms
最佳答案
我不确定您的 Pyramid 日志记录发生了什么,但这是我下载文件的 View 。我目前使用的是 iPhone,所以我只是剪切和粘贴,但您应该明白了。
@view_config(route_name="download_view", renderer="")
def server_view1010(request):
filepath = os.path.join(CFG.home_dir, "static/graphics/imgs/img_sample.jpg")
if request.storage.exists(filepath): #there is no overwrite
response = FileResponse(filepath)
response.headers['Content-Disposition'] = ("attachment; filename=img_sample.jpg")
return response
else:
return Response("Unable to find: {}".format(request.path_info))
关于pyramid - 如何使用 Pyramid FileResponse 提供此视频文件下载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39189609/