本文整理汇总了Python中webob.Response.content_disposition方法的典型用法代码示例。如果您正苦于以下问题:Python Response.content_disposition方法的具体用法?Python Response.content_disposition怎么用?Python Response.content_disposition使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类webob.Response
的用法示例。
在下文中一共展示了Response.content_disposition方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: xls_response
# 需要导入模块: from webob import Response [as 别名]
# 或者: from webob.Response import content_disposition [as 别名]
def xls_response(rows, encoding='ISO-8859-1', filename='response.xls', num_formats=(), col_widths=()):
"""Creates a downloadable webob.Response instance with the given row data
as an XLS (Excel) file.
:param rows: The spreadsheet data as rows.
:type rows: iterable
:param encoding: The character encoding for the data in the spreadsheet.
:type encoding: str
:param filename: The suggested filename for the user to save the file as.
:type filename: str
:param num_formats: A tuple of Excel format strings for numeric values.
:type num_formats: tuple
"""
stream = StringIO()
wb = xlwt.Workbook(encoding=encoding)
ws = wb.add_sheet('Nuorisovaalit 2011')
# Compile the style objects
styles = []
for fmt in num_formats:
if fmt is None:
styles.append(None)
else:
styles.append(xlwt.easyxf(num_format_str=fmt))
# Set column formatting styles.
if len(rows) > 0 and len(num_formats) == len(rows[0]):
for c in xrange(len(rows[0])):
if styles[c] is not None:
ws.col(c).set_style(styles[c])
# Set column widths.
if len(rows) > 0 and len(col_widths) == len(rows[0]):
for c in xrange(len(rows[0])):
if col_widths[c] is not None:
ws.col(c).width = col_widths[c]
for r, row in enumerate(rows):
for c, item in enumerate(row):
if len(styles) == len(row) and styles[c] is not None:
ws.write(r, c, item, styles[c])
else:
ws.write(r, c, item)
wb.save(stream)
response = Response()
response.body = stream.getvalue()
stream.close()
# Set Response headers.
response.content_type = 'application/vnd.ms-excel'
response.content_type_params = {}
response.content_disposition = 'attachment; filename={0}'.format(filename)
return response
示例2: download
# 需要导入模块: from webob import Response [as 别名]
# 或者: from webob.Response import content_disposition [as 别名]
def download(request):
suid = request.matchdict['suid']
rev = request.matchdict.get('rev')
root = get_root()
config = solr_config(root)
query = Term('suid', suid)
if rev:
query = query & Term('revision', rev)
else:
query = query & Term('state', 'active')
query = query & Term('visibility', 'anonymous')
md = Metadata(config, SOLR_FIELDS)
fl = 'effective,expires,physical_path,mimetype,filename'
result = md.query(q=query, fl=fl)
if len(result) != 1:
raise MDBError(u'Dataset not found in SOLR. Query: %s' % query)
md = result[0]
if not chk_publication(md):
raise MDBError(u'Item not effective or already expired')
physical_path = u'/xsendfile%s.binary' % md['physical_path']
response = Response()
response.content_type = md['mimetype']
response.content_disposition = \
'attachment; filename=%s' % md['filename']
response.headers.add('X-Accel-Redirect', physical_path)
return response
示例3: do_download
# 需要导入模块: from webob import Response [as 别名]
# 或者: from webob.Response import content_disposition [as 别名]
def do_download(self, req):
"""
Send the library as a .zip archive
"""
archive = self.library.archive()
size = archive.seek(0, io.SEEK_END)
archive.seek(0)
resp = Response()
resp.content_type = 'application/zip'
resp.content_length = size
resp.content_disposition = 'attachment; filename=images.zip'
resp.app_iter = FileWrapper(archive)
return resp
示例4: csv_response
# 需要导入模块: from webob import Response [as 别名]
# 或者: from webob.Response import content_disposition [as 别名]
def csv_response(rows, encoding='ISO-8859-1', filename='response.csv'):
"""Create a downloadable webob.Response instance with the given
row data as a CSV file.
"""
stream = StringIO()
writer = csv.writer(stream, dialect='excel')
for row in rows:
# Encode all columns in the row since the csv.writer cannot
# handle Unicode objects.
writer.writerow([unicode(s).encode(encoding) for s in row])
response = Response()
response.body = stream.getvalue()
stream.close()
# Set Response headers.
response.charset = encoding
response.content_type = 'text/csv'
response.content_disposition = 'attachment; filename={0}'.format(filename)
return response
示例5: download_csv
# 需要导入模块: from webob import Response [as 别名]
# 或者: from webob.Response import content_disposition [as 别名]
def download_csv(self, request, suffix=''):
response = Response(content_type='text/csv')
response.app_iter = self.get_csv()
response.content_disposition = 'attachment; filename=course_data.csv'
return response
示例6: xls_response_multiple
# 需要导入模块: from webob import Response [as 别名]
# 或者: from webob.Response import content_disposition [as 别名]
def xls_response_multiple(sheets, encoding='ISO-8859-1', filename='response.xls', num_formats=(), col_widths=()):
"""Creates a downloadable webob.Response instance with the given row data
as an XLS (Excel) file.
:param sheets: The spreadsheet data as an iterable of (sheet_name,
rows) tuples with the row data of a sheet in the
rows iterable.
:type sheets: iterable
:param encoding: The character encoding for the data in the spreadsheet.
:type encoding: str
:param filename: The suggested filename for the user to save the file as.
:type filename: str
:param num_formats: A tuple of Excel format strings for numeric values.
:type num_formats: tuple
"""
wb = xlwt.Workbook(encoding=encoding)
# Compile the style objects
styles = []
for fmt in num_formats:
if fmt is None:
styles.append(None)
else:
styles.append(xlwt.easyxf(num_format_str=fmt))
if not sheets:
wb.add_sheet(u'Tyhjä taulukko')
for sheet_name, rows in sheets:
# Worksheet name must not be longer than 31 characters.
if len(sheet_name) > 31:
sheet_name = sheet_name[:31]
# Create a new sheet and write to it.
ws = wb.add_sheet(sheet_name)
# Set column widths.
if len(rows) > 0 and len(col_widths) == len(rows[0]):
for c in xrange(len(rows[0])):
if col_widths[c] is not None:
ws.col(c).width = col_widths[c]
for r, row in enumerate(rows):
for c, item in enumerate(row):
if len(styles) == len(row) and styles[c] is not None:
ws.write(r, c, item, styles[c])
else:
ws.write(r, c, item)
stream = StringIO()
wb.save(stream)
response = Response()
response.body = stream.getvalue()
stream.close()
# Set Response headers.
response.content_type = 'application/vnd.ms-excel'
response.content_type_params = {}
response.content_disposition = 'attachment; filename={0}'.format(filename)
return response