PDF抛出到浏览器控制台,而不是下载 [英] PDF thrown to browser console, not downloading

查看:199
本文介绍了PDF抛出到浏览器控制台,而不是下载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Rotativa 从视图生成PDF文件 ,这很好,但现在在浏览器上我得到了在控制台抛出的原始文件,没有下载对话框,没有警告,没有。这是我的代码:

I'm using Rotativa to generate a PDF file from a view, which works well, but now on the browser I get the raw file thrown at the console, no download dialog box, no warning, nothing. Here's my code:

控制器

public ActionResult DescargarPDF (int itemId) {
        var presupuesto = ReglasNegocio.Fachada.Consultas.ObtenerPresupuesto(itemId);     
        return new Rotativa.PartialViewAsPdf("_PresupuestoFinal", presupuesto) {
            FileName = "Presupuesto_" + itemId + ".pdf",
            PageSize = Rotativa.Options.Size.A4
        };
    }

JQuery脚本:

$(".convertirPDF").on("click", function (id) {
    var itemId = $(this).data('itemid');
    Pdf(itemId);
});

function Pdf(itemid) {
    var id = itemid;

    $.ajax({
        method: "POST",
        url: 'DescargarPDF',
        data: { itemId: id },
        cache: false,
        async: true,
    });
};

HTML上的按钮

<button class="convertirPDF btn btn-secondary btn-info" data-itemid="@item.Id">PDF</button>

我在控制器上尝试了几个代码(结果相同)因为脚本和视图似乎工作得很好。但是,我怀疑,也许html或脚本需要一些调整来通知浏览器它必须下载文件?

I've tried several codes on the controller (with same result) since the script and view seems to work fine. However, I'm suspecting, maybe the html or the script need some tuning to inform the browser it has to download the file?

提前感谢所有人。

推荐答案

我找到了解决方案。它不优雅,但它的工作原理。
所以我不需要使用ajax来发出请求,也不需要为按钮提供功能。我确定这个问题与JS和/或jQuery有关。不过,有一种更简单的方法可以做到这一点。

I found a solution. It's not elegant, but it works. So I didn't need to use ajax necessarily to make the request, neither to give function to the button. I'm kind of sure that the issue has something to do with JS and/or jQuery. Nevertheless, there's a simpler way to do this.

我将我的html按钮更改为:

I changed my html button to:

<a href="DescargarPDF/?itemId=@item.Id" target="_blank" class="btn btn-secondary btn-info">PDF</a>

因此它看起来像一个按钮,但它确实是我控制器方法的链接。我还删除了该按钮的脚本,现在它下载了该文件。没有预定的名称,但仍然。

so it looks like a button but it's really a link to my controller¡s method. I also removed the script for that button and now it downloads the file. Not with the name intended, but still.

感谢大家。快乐的编码。

Thanks to everyone. Happy coding.

更新

我一直在做同一个项目,我想我发现了为什么我的PDF文件被抛入控制台。

I've been working on the same project, and I think I found out why my PDF file was being thrown into console.

事实是,jQuery发出请求,所以jQuery管理响应。就这么简单。如果您查看官方文档 .post(),你会看到以下内容:

The thing is, jQuery makes the request, so jQuery manages the response. Is that simple. If you check official docs for .post(), you'll see the following:


成功的回调函数传递返回的数据,它将是一个XML根目录元素或文本字符串,具体取决于响应的MIME类型。它也传递了响应的文本状态。

The success callback function is passed the returned data, which will be an XML root element or a text string depending on the MIME type of the response. It is also passed the text status of the response.

从jQuery 1.5开始,成功回调函数也传递了一个jqXHR对象(在jQuery 1.4中,它是传递了XMLHttpRequest对象。)

As of jQuery 1.5, the success callback function is also passed a "jqXHR" object (in jQuery 1.4, it was passed the XMLHttpRequest object).

大多数实现都会指定成功处理程序。

Most implementations will specify a success handler.

而我不是,所以,默认情况下,它只是将它放到控制台。我希望这能为问题提供一些启示并提供帮助。快乐的编码。

And I wasn't, so, by default, it just drop it to console. I hope this throws some light into the issue and helps. Happy coding.

这篇关于PDF抛出到浏览器控制台,而不是下载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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