【发布时间】:2014-08-06 23:35:24
【问题描述】:
如何让我的 Asp.net webapi 返回正确的 pdf 表单提交响应?
【问题讨论】:
标签: asp.net pdf asp.net-web-api
如何让我的 Asp.net webapi 返回正确的 pdf 表单提交响应?
【问题讨论】:
标签: asp.net pdf asp.net-web-api
要将响应保留在 pdf 应用程序中,您必须返回 FDF 格式的文件。
让这个工作的关键是你不能只返回一个字符串。您必须先将其编码为内存流,并且标头必须设置为application/vnd.fdf。
示例代码如下:
Public Function PostValue(<FromBody()> ByVal value As MyPDFFieldsObject) As HttpResponseMessage
Dim fdfmessage As String = "%FDF-1.2" & vbCrLf & _
"1 0 obj <<" & vbCrLf & _
"/FDF <<" & vbCrLf & _
"/Status (Submitted Successfully!)" & vbCrLf & _
">>" & vbCrLf & _
">>" & vbCrLf & _
"endobj" & vbCrLf & _
"trailer" & vbCrLf & _
"<</Root 1 0 R>>" & vbCrLf & _
"%%EOF"
Dim result As HttpResponseMessage = New HttpResponseMessage(HttpStatusCode.OK)
Dim stream As New MemoryStream(Encoding.UTF8.GetBytes(fdfmessage))
stream.Position = 0
result.Content = New StreamContent(stream)
result.Content.Headers.ContentType = New MediaTypeHeaderValue("application/vnd.fdf")
Return result
End Function
【讨论】: