如何从mvc部分视图下载pdf ?

时间:2021-08-17 09:48:09

I've created a PdfActionResult class as follows:

我创建了一个PdfActionResult类,如下所示:

public class PdfActionResult : ActionResult
{
    public byte[] FileContents { get; set; }
    public string FileName { get; set; }


    public override void ExecuteResult(ControllerContext context)
    {
        var cd = new System.Net.Mime.ContentDisposition()
                     {
                        FileName = FileName,
                        Inline = false,
                     };

        context.HttpContext.Response.Buffer = true;
        context.HttpContext.Response.Clear();
        context.HttpContext.Response.AppendHeader("Content-Disposition", cd.ToString());
        context.HttpContext.Response.ContentType = "application/pdf";
        context.HttpContext.Response.BinaryWrite(FileContents);
    }

}

I have a controller method that returns a PdfActionResult. This works fine when called from within a view but it fails when called from a partial view. My guess is that it has something to do with the Controller context. Any help would be appreciated. Thanks.

我有一个返回PdfActionResult的控制器方法。当从视图中调用时,这种方法可以正常工作,但在从局部视图调用时失败。我猜它与控制器上下文有关。如有任何帮助,我们将不胜感激。谢谢。

2 个解决方案

#1


1  

Sorry for not answering your question more directly in my comments.

抱歉没有在我的评论中更直接地回答你的问题。

What do you expect to happen by putting this in a partial view? Basically, with files, it's an all or nothing proposition. As soon as you return a FileResult, it's game over. No page is rendered, and the only thing that happens is that a file is streamed over to the client's browser. Basically, in a HTTP stream you can only have one content type -- either it's an HTML document or it's another type of file.

把这个放到局部视图中会发生什么?基本上,对于文件来说,这是一个要么全部要么没有的命题。一旦你返回一个文件,游戏就结束了。不会呈现任何页面,惟一的情况是文件被流到客户机的浏览器。基本上,在HTTP流中,只能有一种内容类型——要么是HTML文档,要么是另一种类型的文件。

If what you really want is to have a page with custom markup which happens to show a PDF inside a frame, I suggest using an <iframe> tag, then pointing the source of that's iFrame tag to be an action which returns the file.

如果你真正想要的是有一个页面,其中包含自定义标记恰好显示PDF框架内,我建议使用一个< iframe >标记,然后指向源的iframe标签是一个操作返回的文件。

#2


0  

Turns out the problem was not the partial view but the fact that I was using an Ajax.BeginForm. It was attempting the download in the Ajax context and that's what caused it to fail. Changing to Html.BeginForm did the trick - hopefully this helps someone in the future.

事实证明,问题不是部分观点,而是我使用的是Ajax.BeginForm。它正在Ajax上下文中尝试下载,这就是导致它失败的原因。改变到Html。开始做这个魔术——希望这能帮助将来的某个人。

#1


1  

Sorry for not answering your question more directly in my comments.

抱歉没有在我的评论中更直接地回答你的问题。

What do you expect to happen by putting this in a partial view? Basically, with files, it's an all or nothing proposition. As soon as you return a FileResult, it's game over. No page is rendered, and the only thing that happens is that a file is streamed over to the client's browser. Basically, in a HTTP stream you can only have one content type -- either it's an HTML document or it's another type of file.

把这个放到局部视图中会发生什么?基本上,对于文件来说,这是一个要么全部要么没有的命题。一旦你返回一个文件,游戏就结束了。不会呈现任何页面,惟一的情况是文件被流到客户机的浏览器。基本上,在HTTP流中,只能有一种内容类型——要么是HTML文档,要么是另一种类型的文件。

If what you really want is to have a page with custom markup which happens to show a PDF inside a frame, I suggest using an <iframe> tag, then pointing the source of that's iFrame tag to be an action which returns the file.

如果你真正想要的是有一个页面,其中包含自定义标记恰好显示PDF框架内,我建议使用一个< iframe >标记,然后指向源的iframe标签是一个操作返回的文件。

#2


0  

Turns out the problem was not the partial view but the fact that I was using an Ajax.BeginForm. It was attempting the download in the Ajax context and that's what caused it to fail. Changing to Html.BeginForm did the trick - hopefully this helps someone in the future.

事实证明,问题不是部分观点,而是我使用的是Ajax.BeginForm。它正在Ajax上下文中尝试下载,这就是导致它失败的原因。改变到Html。开始做这个魔术——希望这能帮助将来的某个人。