代码之家  ›  专栏  ›  技术社区  ›  Gabe

将ASP.NET WebForms逻辑转换为ASP.NET MVC

  •  2
  • Gabe  · 技术社区  · 16 年前

    我在一个旧的ASP.NET webforms应用程序中使用了此代码, MemoryStream 并将其作为显示PDF作为响应的响应传递。我现在正在使用一个ASP.NET MVC应用程序,并希望做同样的事情,但是我应该如何开始显示 内存流 以PDF格式使用MVC?

    以下是我的ASP.NET WebForms代码:

        private void ShowPDF(MemoryStream ms)
        {
            try
            {
                //get byte array of pdf in memory
                byte[] fileArray = ms.ToArray();
                //send file to the user
                Page.Response.Cache.SetCacheability(HttpCacheability.NoCache);
                Page.Response.Buffer = true;
                Response.Clear();
                Response.ClearContent();
                Response.ClearHeaders();
                Response.Charset = string.Empty;
                Response.ContentType = "application/pdf";
                Response.AddHeader("content-length", fileArray.Length.ToString());
                Response.AddHeader("Content-Disposition", "attachment;filename=TID.pdf;");
                Response.BinaryWrite(fileArray);
                Response.Flush();
                Response.Close();
            }
            catch
            {
               // and boom goes the dynamite...
            }
        }
    
    1 回复  |  直到 16 年前
        1
  •  2
  •   Dave Swersky    16 年前

    这是一篇关于以下内容的博客文章: http://biasecurities.com/blog/2008/binaryresult-for-asp-net-mvc/

    更新:文章的最后一条评论提到了response.transmitfile,如果您的pdf很大,并且有很多并发下载,那么您可能需要调整代码来使用它。