简体   繁体   English

提供动态生成的文件而不是 htm

[英]Serve dynamically generated file instead of htm

I'm completely new to twisted and need a little push forward.我对扭曲完全陌生,需要一点点推动。 I'm looking for a way to serve a dynamically generated file other than htm page - for example csv file.我正在寻找一种方法来提供 htm 页面以外的动态生成文件 - 例如 csv 文件。

EDIT: It happened that while change of 'content-type' would suffice with csv file, what I needed was a change of 'content-disposition' of request:编辑:碰巧虽然更改“内容类型”就足以使用 csv 文件,但我需要的是更改请求的“内容处理”:

request.responseHeaders.setRawHeaders('content-disposition', ['attachment; filename="file.csv"'])

设置正确的Content-type标头。

request.responseHeaders.setRawHeaders('content-type', ['text/csv'])

The basic idea how to do this is to generate HTTP response like this:如何做到这一点的基本思想是生成这样的 HTTP 响应:

...
Content-Type: text/csv; charset=utf-8

1,2,3,4
...

I don't know whether you are familiar how does raw HTTP request look, but it consists of two major fields:不知道大家是否熟悉原始HTTP请求的样子,但它主要由两个字段组成:

  • header标题
  • body (html)正文 (html)

And they are separated by \\r\\n\\r\\n .它们由\\r\\n\\r\\n分隔。 So you just have to send new Content-Type header (set to text/csv ) and then binary content instead of HTML.因此,您只需发送新的Content-Type标头(设置为text/csv ),然后发送二进制内容而不是 HTML。

In twisted it's apparently possible trough setHeader method.在扭曲中,显然可以通过setHeader方法实现。

Edit - additional HTTP info based on comment编辑 - 基于评论的附加 HTTP 信息

You could take look at:你可以看看:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM