ASP.Net WebAPI Return value for PDF Form Submit

寵の児 提交于 2020-03-05 07:05:44

问题


How do I get my Asp.net webapi to return the correct response for a pdf form submit?


回答1:


To keep the response inside of the pdf application you must return an FDF formatted file.

The key to get this to work is that you cannot just return a string. You must first encode it to a memory stream and the header must be set to application/vnd.fdf.

Here is the example code:

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


来源:https://stackoverflow.com/questions/25171999/asp-net-webapi-return-value-for-pdf-form-submit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!