2

go-swagger用来下载附件。这些是小的多行文件,另一端只有一个浏览器。我尝试将响应定义为'string',但找不到用多行文本填充有效负载的方法,它以“\r\n”而不是换行符到达。我也尝试了'string'format 'binary',但随后客户端看到一个包含Reader{}. 我yaml的 200 响应内容如下所示:

headers:
        Content-Disposition:
          type: string
          pattern: attachment; filename="attachement.txt"
        Content-Type:
          type: string
          pattern: application/octet-stream
      schema:
        type: string

我也尝试了'string'format 'byte',但我不想要base64编码响应。对此有何建议?

这是我到目前为止所尝试的:

尝试“字符串”格式“字节”...

  payload := bytes.NewBufferString("first line")
    payload.WriteByte(13)
    payload.WriteByte(10)
    payload.WriteString("second line")
    payload.WriteByte(13)
    payload.WriteByte(10)
    resp := responses.NewGetResponseInfoOK()
    resp.SetPayload(payload)
  // fails.. will not accept payload other than strfmt.Bas64

尝试“字符串”

payload := bytes.NewBufferString("first line")
    payload.WriteByte(13)
    payload.WriteByte(10)
    payload.WriteString("second line")
    payload.WriteByte(13)
    payload.WriteByte(10)
    resp := responses.NewGetResponseInfoOK()
    resp.SetPayload(payload.String())
  // accepts payload, but 13/10 get converted into \r\n

尝试“字符串”格式“二进制”

type nopCloser struct {
      io.Reader
  }

  func (nopCloser) Close() error { return nil }


  payload := bytes.NewBufferString("first line")
    payload.WriteByte(13)
    payload.WriteByte(10)
    payload.WriteString("second line")
    payload.WriteByte(13)
    payload.WriteByte(10)
    resp := responses.NewGetResponseInfoOK()
    resp.SetPayload(nopCloser(payload))

  // accepts payload, but the browser sees a Reader{}
4

1 回答 1

2

为了重申对这个问题的评论,任何试图简单地创建一个允许在 go-swagger 中下载文件的端点的人,只需produces application/octet-stream在方法中添加一个。

于 2018-11-13T03:23:10.910 回答