ASP.Net WebAPI PDF表单提交的返回值

时间:2014-08-06 23:35:25

标签: asp.net pdf asp.net-web-api

如何让我的Asp.net webapi返回正确的pdf表单提交回复?

1 个答案:

答案 0 :(得分:0)

要将响应保留在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