如何允许文件的下载,即返回由AJAX二进制数据 [英] How to allow download of file, that is returned as binary data from AJAX

查看:605
本文介绍了如何允许文件的下载,即返回由AJAX二进制数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是,我发送给客户端的第一个PDF下载,然后我需要检查,如果存在于我的数据库中的一些数据,然后根据所检查我需要证明的问题,如果用户想下载其它PDF,那我产生。

我的code:

  //在这里,我只是做了询问对话框
 $('#printDWInfo)。对话框({
            可调整大小:假的,
            模式:真正的,
            的AutoOpen:假的
        });

 //这里是我的问题:)
 $('#generujWydruk)。点击(函数(事件){
            。事件preventDefault();
            $('#printForm)提交()。 //<  - 发送第一个请求,客户端就取得第一PFD文件
            $。员额('<%:RESOLVEURL(〜/报告/ KPiRReportDWCheck)%>,<  - 检查另一个数据
                $(#printForm)。序列化()
                功能(数据){
                    如果(data.length大于0){
                        $(#printDWInfo)。对话框(选项,按钮,[
                            {
                                文字:德,
                                点击:函数(){
                                    $阿贾克斯({类型:POST,
                                        网址:'<%= Url.Action(PrintDWList,报告)%>',
                                        数据类型:JSON,
                                        传统:真正的,
                                        数据:{IDS:数据},
                                        成功:函数(数据2){
                                            //我不知道该怎么办这里
                                        }
                                    });
                                    $(本).dialog(亲密);
                                    }
                                },{
                                文字:聂
                                点击:函数(){
                                    $(本).dialog(亲密);
                                    }
                            }
                        ]);
                        $('#printDWInfo)对话框(开放)。
                    }
                }
            );
 

如果按钮客户端点击德,在对话框中,我使用Ajax请求,因为我可以传递给CONTROLER整型数组,由 $返回后('<%:RESOLVEURL( 〜/报告/ KPiRReportDWCheck)%> 。 在我的Ajax请求萤火成功函数告诉我,数据2 是我的PDF文件的二进制数据,我需要做的,让客户端下载该PDF文件?

解决方案
  

在我的Ajax请求萤火成功函数告诉我,DATA2是   我的PDF文件的二进制数据,我需要做的,让客户端   下载该PDF文件?

您不应该使用AJAX来下载文件。问题是,你获取的PDF字节在你的AJAX调用成功回调的JavaScript变量,但有你可以用它做什么。你可以不提示用户保存它,当然你不能将其保存到客户端,因为JavaScript没有必要的特权。

所以,你应该使用一个普通的请求:

  VAR downloadUrl ='<%= Url.Action(PrintDWList,报告)%>? + $ .PARAM({IDS:数据},真正的);
window.location.href = downloadUrl;
 

请注意,这会发送一个GET请求到 PrintDWList 动作传递的ID查询字符串参数,以便确保这个动作是GET访问。现在,如果控制器操作使用内容处置头附件它将提供用户下载文件:

 公众的ActionResult PrintDWList(INT [] IDS)
{
    byte []的PDF = ...
    返回文件(PDF,应用程序/ PDF格式,file.pdf);
}
 

My problem is that I send to client first PDF to download, then I need to check, if some data exists in my database, then depending on that check I need to show question that if user want to download another PDF, that I generates.

My Code:

 //Here I just make dialog for question
 $('#printDWInfo').dialog({
            resizable: false,
            modal: true,
            autoOpen: false
        });

 //Here is my problem :)
 $('#generujWydruk').click(function (event) {
            event.preventDefault();
            $('#printForm').submit(); // <-- sending first request and client get first PFD file
            $.post('<%: ResolveUrl("~/Reports/KPiRReportDWCheck") %>', <-- check for another data
                $("#printForm").serialize(),
                function(data) {
                    if (data.length > 0) {
                        $("#printDWInfo").dialog( "option", "buttons", [
                            {
                                text: "Tak",
                                click: function () {
                                    $.ajax({ type: "POST",
                                        url: '<%= Url.Action("PrintDWList","Reports")%>',
                                        datatype: "json",
                                        traditional: true,
                                        data:{'ids': data },
                                        success: function (data2) {
                                            //I don't know what to do here
                                        }
                                    });
                                    $(this).dialog("close");
                                    }
                                }, {
                                text: "Nie",
                                click: function () {
                                    $(this).dialog("close");
                                    }
                            }
                        ]);
                        $('#printDWInfo').dialog("open");
                    }
                }
            );

If client click on button "Tak" in dialog, I use ajax request, because I can pass to controler array of int, that is returned by $.post('<%: ResolveUrl("~/Reports/KPiRReportDWCheck") %>'. In success function of my ajax request FireBug show me that data2 is binary data of my PDF file, what I need to do to allow client to download this PDF file?

解决方案

In success function of my ajax request FireBug show me that data2 is binary data of my PDF file, what I need to do to allow client to download this PDF file?

You should not use AJAX to download files. The problem is that you are fetching the pdf bytes in a javascript variable in the success callback of your AJAX call but there's nothing you could do with it. You cannot prompt the user to save it and of course you cannot save it to the client since javascript doesn't have the necessary privileges.

So you should use a normal request:

var downloadUrl = '<%= Url.Action("PrintDWList", "Reports")%>?' + $.param({ ids: data }, true);
window.location.href = downloadUrl;

Notice that this will send a GET request to the PrintDWList action passing the ids query string parameter so make sure this action is accessible on GET. Now if the controller action uses the Content-Disposition header to attachment it will offer the user to download the file:

public ActionResult PrintDWList(int[] ids)
{
    byte[] pdf = ...
    return File(pdf, "application/pdf", "file.pdf");
}

这篇关于如何允许文件的下载,即返回由AJAX二进制数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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