相关疑难解决方法(0)

在asp.net/mvc中的控制器内的动作中为http响应添加标头

我正在将数据从服务器传输到客户端以供下载使用filestream.write.在这种情况下,发生的事情是我能够下载该文件,但它不会在我的浏览器中显示为下载."另存为"弹出窗口不会出现在"下载"部分中的"下载栏"中.从四处查看,我想我需要在响应标题中包含"something"来告诉浏览器这个响应有一个附件.我也想设置cookie.要做到这一点,这就是我在做的事情:

        [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");
    }
Run Code Online (Sandbox Code Playgroud)

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

我在正确的位置更新标题吗?有没有其他方法可以做到这一点?

c# asp.net-mvc response httpcontext

11
推荐指数
2
解决办法
5万
查看次数

标签 统计

asp.net-mvc ×1

c# ×1

httpcontext ×1

response ×1