当前位置: 首页>>代码示例>>Python>>正文


Python Response.content_disposition方法代码示例

本文整理汇总了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
开发者ID:verkkodemokratiaseura,项目名称:verkkovaali,代码行数:62,代码来源:school.py

示例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
开发者ID:bluedynamics,项目名称:cone.mdb,代码行数:28,代码来源:public.py

示例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
开发者ID:EaSonic,项目名称:picroscopy,代码行数:15,代码来源:wsgi.py

示例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
开发者ID:verkkodemokratiaseura,项目名称:verkkovaali,代码行数:23,代码来源:school.py

示例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
开发者ID:kriwil,项目名称:xblock-mentoring,代码行数:7,代码来源:dataexport.py

示例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
开发者ID:verkkodemokratiaseura,项目名称:verkkovaali,代码行数:68,代码来源:school.py


注:本文中的webob.Response.content_disposition方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。