如何立即开始下载(但不能分块数据)?

2024-03-29 10:54:48 发布

您现在位置:Python中文网/ 问答频道 /正文

我有这样的看法:

def download(request):
    response = StreamingHttpResponse([save_virtual_workbook(make_xl_workbook())], content_type="application/vnd.ms-excel")
    response[u'Content-Disposition'] = u'attachment; filename="%s"' % filename
    return response

但它会等待提示用户下载,直到make\u xl\u工作簿运行之后。你知道吗

我读到我不能把make\u xl\u工作簿返回的数据分块出来,因为xlsx文件是压缩的。所以我希望马上开始下载,然后运行函数,然后在函数运行后将完整的数据注入到响应中。你知道吗

我也试过这个,但似乎没用。它仍然在下载开始前运行make\u xl\u工作簿的完整功能。你知道吗

def download(request):
    def save_xl():
        yield save_virtual_workbook(make_xl_workbook())
    response = StreamingHttpResponse(save_xl(), content_type="application/vnd.ms-excel")
    response[u'Content-Disposition'] = u'attachment; filename="%s"' % filename
    return response

更新:也尝试了这个,得到同样的行为,没有改变。你知道吗

def download(request):
    def save_xl():
        yield ''
        yield save_virtual_workbook(make_xl_workbook())
    response = StreamingHttpResponse(save_xl(), content_type="application/vnd.ms-excel")
    response[u'Content-Disposition'] = u'attachment; filename="%s"' % filename
    return response

Tags: makeapplicationresponserequestdownloadsavedeftype