从ASP.NET操作返回文件时出现ERR_SPDY_PROTOCOL_ERROR [英] ERR_SPDY_PROTOCOL_ERROR when returning file from ASP.NET action

查看:730
本文介绍了从ASP.NET操作返回文件时出现ERR_SPDY_PROTOCOL_ERROR的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have some old Web API action method which returns CSV file. It worked for long time, but recently stopped. Now it causes ERR_SPDY_PROTOCOL_ERROR.

ERR_SPDY_PROTOCOL_ERROR通常与Avast安全性相关联. >.但是,就我而言,这不是由Avast引起的,其他Web浏览器也会引发异常.

ERR_SPDY_PROTOCOL_ERROR in Chrome is often associated with Avast security as described here. In my case however it's not caused by Avast, and other web browsers throw exceptions too.

我的操作方法如下:

[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,即可通过有角前端调用该方法.

The method is called by angular front end by simple change of window.location on button click.

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

Whole action method is executed properly, with no exceptions. Error is shown only by web browser.

此处中所述的在Chrome中刷新套接字解决问题.

Flushing sockets in Chrome as described here does not solve the issue.

推荐答案

我已经在API控制器中尝试了此方法,并通过chrome浏览器调用,它抛出了net::ERR_CONNECTION_RESET

I have tried this method in API controller and call through chrome browser, it throws net::ERR_CONNECTION_RESET

响应中存在一些问题,其中填充了StreamContent,在结果内容中使用ByteArrayContent,效果很好.

There is some issue in response filled with StreamContent, use ByteArrayContent in result content, it works perfectly.

    [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;
        }
    }

这篇关于从ASP.NET操作返回文件时出现ERR_SPDY_PROTOCOL_ERROR的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆