本文整理匯總了Python中pyramid.response.FileResponse.set_cookie方法的典型用法代碼示例。如果您正苦於以下問題:Python FileResponse.set_cookie方法的具體用法?Python FileResponse.set_cookie怎麽用?Python FileResponse.set_cookie使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類pyramid.response.FileResponse
的用法示例。
在下文中一共展示了FileResponse.set_cookie方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: artifact_download
# 需要導入模塊: from pyramid.response import FileResponse [as 別名]
# 或者: from pyramid.response.FileResponse import set_cookie [as 別名]
def artifact_download(request, inline):
af = get_artifact(request)
if af.is_bundle:
if inline:
raise HTTPBadRequest("Inline view not supported for bundles")
# We have a bundle. So we need to prepare a zip (unless we already have one)
disk_name = af.file
# Locking on a separate file since zipfile did not want to write to the
# same file we are locking on. It just means that we need to clean up
# that file at the same time as the main cache file.
with portalocker.Lock(disk_name + ".lock", timeout=300, check_interval=1):
if not os.path.exists(disk_name):
with zipfile.ZipFile(disk_name, 'w', compression=zipfile.ZIP_BZIP2) as _zip:
for cf in af.artifacts:
_zip.write(cf.file, arcname=cf.bundle_filename(af))
file_name = af.name + ".zip"
else:
disk_name = af.file
file_name = af.filename
mime, encoding = mimetypes.guess_type(file_name)
if mime is None:
mime = ('text/plain' if inline else 'application/octet-stream')
# If the current simple approach proves to be a problem the discussion
# at http://stackoverflow.com/q/93551/11722 can be considered.
response = FileResponse(disk_name, request=request, content_type=mime)
response.content_disposition = '{}; filename="{}"'.format('inline' if inline else 'attachment', file_name)
# Specifically needed for jquery.fileDownload
response.set_cookie('fileDownload', 'true')
return response