0

我有一个 MVC3 应用程序,它允许用户将文件下载到他们的硬盘驱动器或在浏览器中查看它。该文件存储在 Internet 无法访问的文件服务器上。所以文件的字节数组存储在内存中,然后像这样发送到浏览器:

            /// <summary>
    /// overrides actionresult method so document can be viewed in browser window
    /// </summary>
    /// <param name="context"></param>
    public override void ExecuteResult(ControllerContext context)
    {
        // get current context and set values
        var response = context.HttpContext.Response;
        response.Clear();
        response.BufferOutput = false;   // to prevent buffering 
        response.Cache.SetCacheability(HttpCacheability.NoCache);
        response.ContentType = ContentType;
        response.AddHeader("Content-Length", Length.ToString());

        var ext = Path.GetExtension(FileName);

        if (!Show)
        response.AddHeader("content-disposition", "attachment; filename=\"" + FileName+ "\"");

        response.ContentEncoding = System.Text.Encoding.UTF8;

        var stream = new MemoryStream(ContentBytes);
        stream.WriteTo(response.OutputStream);
        stream.Dispose();

    }

由于某种原因,我无法查看 pdf。我可以下载它们但不能查看它们。我在这里附加了一个示例项目showinbrowser.zip。它的VS2010 sp1。有人可以告诉我如何在存储在内存中的新浏览器窗口/选项卡中显示 PDF 文件吗?谢谢

4

2 回答 2

2

为什么不使用Controller.File为您处理此问题的方法?

public ActionResult _ShowDocument(string id)
{
  byte[] fileData=GetByteArrayFilefromId(id);
  return File(fileData,"application/pdf","somefile.pdf");
}
于 2012-08-17T18:35:45.653 回答
1

嗨,将您的内容类型设置为 pdf

//Set the appropriate ContentType.
     Response.ContentType = "Application/pdf";
于 2012-08-17T18:33:02.997 回答