小编arv*_*ind的帖子

尝试通过 Web api 调用下载文件时,如何解决“发送 HTTP 标头后服务器无法设置状态”错误?

当我使用 Web api 调用下载文件时,我可以轻松下载该文件。唯一的问题是,在我的错误日志中发送 HTTP 标头后,我收到“服务器无法设置状态”。抱歉,如果这可能是重复的问题,但这里的答案都没有帮助我。

<a href="/api/DownloadDocumentById?documentId=<%=doc.Id %>" download>
                                    <i class="fa fa-download text-primary"></i>
                                </a>
Run Code Online (Sandbox Code Playgroud)
<HttpGet>
    <ActionName("DownloadDocumentById")>
    Public Function DownloadDocumentById(documentId As Integer)
        Dim document = xxxxxxxx

        Dim context = HttpContext.Current

        context.Response.ContentType = document.Type

        context.Response.OutputStream.Write(document.Content, 0, document.Size)

        context.Response.AddHeader("Content-Disposition", Baselib.FormatContentDispositionHeader($"{document.Name}"))

        context.Response.AddHeader("Last-Modified", DateTime.Now.ToLongDateString())

        context.Response.Flush()

        context.Response.End()

        Return HttpStatusCode.OK // Have also tried to create a sub without returning a value
    End Function
Run Code Online (Sandbox Code Playgroud)

如前所述,我可以轻松下载该文档,但 IIS 仍然记录“HTTP 标头已发送后服务器无法设置状态”错误。再次抱歉,这是一个重复的问题。希望可以有人帮帮我。

vb.net webforms download asp.net-web-api asp.net-apicontroller

0
推荐指数
1
解决办法
9281
查看次数