【问题标题】:ERR_SPDY_PROTOCOL_ERROR when returning file from ASP.NET action从 ASP.NET 操作返回文件时出现 ERR_SPDY_PROTOCOL_ERROR
【发布时间】:2017-08-14 15:23:48
【问题描述】:

我有一些返回 CSV 文件的旧 Web API 操作方法。它工作了很长时间,但最近停止了。现在它会导致 ERR_SPDY_PROTOCOL_ERROR。

Chrome 中的 ERR_SPDY_PROTOCOL_ERROR 通常与 here 所述的 Avast 安全性相关联。但就我而言,这不是由 Avast 引起的,其他网络浏览器也会抛出异常。

我的操作方法如下:

[HttpGet]
[Route("csv")]
public HttpResponseMessage SomeMethod([FromUri]SomeSearchCriteria sc)
{
    using (MemoryStream stream = new MemoryStream())
    {
        StreamWriter writer = new StreamWriter(stream, Encoding.UTF8);
        string content = someLogic.SomeSearchmethod(sc);
        writer.Write(content);
        writer.Flush();
        stream.Position = 0;

        HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK);
        result.Content = new StreamContent(stream);
        result.Content.Headers.ContentType = new MediaTypeHeaderValue("text/csv");
        result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment") { FileName = "Export.csv" };
        return result;
    }              
}

该方法由角前端通过在按钮单击时简单更改window.location来调用。

整个动作方法都正确执行,没有例外。错误仅由网络浏览器显示。

here 所述在 Chrome 中刷新套接字并不能解决问题。

【问题讨论】:

  • 您在哪些浏览器(以及这些浏览器的版本)中遇到此错误?
  • Chrome 中的这个特定错误(据我所知是最新的),但在 IE 和 Firefox 中也有错误,但是没有错误消息。

标签: c# asp.net angularjs asp.net-web-api asp.net-web-api2


【解决方案1】:

我在 API 控制器中尝试过这种方法并通过 chrome 浏览器调用,它会抛出 net::ERR_CONNECTION_RESET

有一些问题以StreamContent 填充,在结果内容中使用ByteArrayContent,它可以正常工作。

    [HttpGet]
    [Route("csv")]
    public HttpResponseMessage SomeMethod([FromUri]SomeSearchCriteria sc)
    {
        using (MemoryStream stream = new MemoryStream())
        {
            StreamWriter writer = new StreamWriter(stream, Encoding.UTF8);
            string content = "test";
            writer.Write(content);
            writer.Flush();
            stream.Position = 0;

            HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK);
            //result.Content = new StreamContent(stream);
            result.Content = new ByteArrayContent(stream.ToArray());
            result.Content.Headers.ContentType = new MediaTypeHeaderValue("text/csv");
            result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment") { FileName = "Export.csv" };
            return result;
        }
    }

【讨论】:

  • 完美。谢谢你:)。
  • 但是为什么?是否应该向 ASPNet 提出问题?
猜你喜欢
  • 2017-10-30
  • 2019-05-06
  • 1970-01-01
  • 1970-01-01
  • 2011-01-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-04-28
相关资源
最近更新 更多