加上头的HTTP响应在行动asp.net/mvc一个控制器内 [英] adding header to http response in an action inside a controller in asp.net/mvc

查看:1509
本文介绍了加上头的HTTP响应在行动asp.net/mvc一个控制器内的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从服务器中使用流式数据到客户端的下载 filestream.write 。在这种情况下所发生的事情是,我能下载文件,但它不显示为下载在我的浏览器。无论是弹出的另存为没有出现下载吧出现在下载部分。环视,我想我需要包括在响应头东西告诉浏览器,有这种反应的附件。此外,我想设置cookie。要做到这一点,这是我在做什么:

I am streaming data from server to client for download using filestream.write. In that case what is happening is that I am able to download the file but it does not appear as download in my browser. Neither the pop-up for "Save As" appears not "Download Bar" appears in Downloads section. From looking around, I guess I need to include "something" in the response header to tell the browser that there is an attachment with this response. Also I want to set the cookie. To accomplish this, this is what I am doing:

        [HttpContext.Current.Response.AppendHeader("Content-Disposition","attachment;filename=" & name)]
    public ActionResult Download(string name)
    {
          // some more code to get data in inputstream.

          using (FileStream fs = System.IO.File.OpenWrite(TargetFile))
            {
                byte[] buffer = new byte[SegmentSize];
                int bytesRead;
                while ((bytesRead = inputStream.Read(buffer, 0, SegmentSize)) > 0)
                {
                    fs.WriteAsync(buffer, 0, bytesRead);
                }
            }
        }
        return RedirectToAction("Index");
    }

我收到错误认为:System.web.httpcontext.current是一个属性,并用作一个类型。

I am getting error that: "System.web.httpcontext.current is a property and is used as a type."

我做的头在正确的位置更新?是否有任何其他方式做到这一点?

Am I doing the header updating at the right place? Is there any other way to do this?

推荐答案

是的,你是做了错误的方式尝试这个,你不应该添加页眉您的行动作为内部的一个属性头的方法。

Yes, You are doing it the wrong way try this, you should add the header inside your action not as an attribute header to your method.

HttpContext.Current.Response.AppendHeader("Content-Disposition","attachment;filename=" & name)

Request.RequestContext.HttpContext.Response.AddHeader("Content-Disposition", "Attachment;filename=" & name)

更新
据我了解,你正在为你的控制器/动作Ajax调用它直接调用的动作不会工作的文件下载。你可以实现这种方式。

Update As i understand you are making an ajax call to your controller/action which wont work for file download by directly calling an action. You can achieve it this way.

public void Download(string name)
        {
//your logic. Sample code follows. You need to write your stream to the response.

            var filestream = System.IO.File.ReadAllBytes(@"path/sourcefilename.pdf");
            var stream = new MemoryStream(filestream);
            stream.WriteTo(Response.OutputStream);
            Response.AddHeader("Content-Disposition", "Attachment;filename=targetFileName.pdf");
            Response.ContentType = "application/pdf";
        }

    public FileStreamResult Download(string name)
    {
        var filestream = System.IO.File.ReadAllBytes(@"path/sourcefilename.pdf");
        var stream = new MemoryStream(filestream);


        return new FileStreamResult(stream, "application/pdf")
        {
            FileDownloadName = "targetfilename.pdf"
        };
    }

在您的JS点击按钮,你可以做一些类似的。

In your JS button click you can just do something similar to this.

 $('#btnDownload').click(function () {
            window.location.href = "controller/download?name=yourargument";
    });

这篇关于加上头的HTTP响应在行动asp.net/mvc一个控制器内的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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