如何使用金字塔FileResponse提供此视频文件以供下载

时间:2016-08-28 09:04:33

标签: pyramid kotti

请问我错过了什么。当我想要提供已下载到磁盘上的视频文件时,我继续在浏览器中获取内部服务器错误。 这是我的代码:视图函数

@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

1 个答案:

答案 0 :(得分:2)

我不确定你的Pyramid日志记录是怎么回事,但这是我下载文件的观点。我目前在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))