文件下载弹出窗口不工作

file download popup is not working

本文关键字:工作 窗口 文件下载      更新时间:2023-09-26

我正在做ASP。NET Web表单应用程序。我的要求是显示excel文件下载弹出框,当用户点击给定的链接(此链接是在poppage不是在aspx页面。)。我有一个链接的aspx页面。当用户点击时,它会调用js函数,我们通过调用web service方法来生成弹出页面的html。

:

 function showListModelFromGenrator(divId) {
    var lowner = $("#" + hdnLoggedInOwnerID)[0].value;
    var sowner = $("#" + hdnSelectedOwnnerID)[0].value;
    var commID = $("#" + hdnCommunityId)[0].value;

    var controlid = '#' + divId;
    $.ajax({
        url: baseUrl + '/' + "WebServices/ExtraInfoWebService.asmx/GetProductActivityStatus",
        data: { LoggedInOwnerId: lowner, SelectedOwnerId: sowner, CommunityId: commID },
        success: function (response) {
            $(controlid).dialog("destroy");
            $(controlid).dialog({
                autoOpen: false,
                modal: true,
                width: 560,
                height: 370,
                resizable: false
            }).empty().append(response.text);
            $(controlid).dialog('open');
            var busyBox = new BusyBoxWrapper()
            busyBox.HideBusyBoxAfter(5);
        },
        cache: false
    });
}
<<p> web方法/strong>:
[WebMethod(EnableSession = true)]
    public string GetProductActivityStatus(int LoggedInOwnerId, int SelectedOwnerId, int CommunityId)
    {
        StringBuilder stringAuditStatus = new StringBuilder();
        Audit objdata = new Audit();
        try
        {
            DataTable dt = new DataTable();
            int ownerID = LoggedInOwnerId;
            if (SelectedOwnerId != 0)
                ownerID = SelectedOwnerId;
            dt = objdata.GetListmodeldata(ownerID, CommunityId);
            stringAuditStatus.Append(@"<table><tr class=addressTableHeader><td>Code</td>" +
                                            "<td>Description</td>" +
                                            "<td>Status</td>" +
                                            "<td>Date</td></tr>");
            foreach (DataRow item in dt.Rows)
            {
                stringAuditStatus.Append(

                                            "<tr><td>" + item["Code"] + "</td>" +
                                            "<td>" + item["Description"] + "</td>" +
                                            "<td>" + item["Status"] + "</td>" +
                                            "<td>" + item["Date"] + "</td></tr>");
            }
            stringAuditStatus.Append("</table>");
            stringAuditStatus.Append("<a id=lnkViewProductCodeStatus runat='Server' href='#' onclick='javascript:ExportExel();'>DownloadListModel</a>");

        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
        return stringAuditStatus.ToString();
    }

当用户点击"lnkViewProductCodeStatus "(上面由web方法创建)。我们调用JS函数ExportExcel,它调用handler方法来处理文件。

function ExportExel(){
    var abc;
    $.ajax({
        type: "POST",
        url: baseUrl + '/' + "WebServices/ExtraInfoWebService.asmx/Urlhttphandler",
        data: {},
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (msg) {
            //window.open(msg.d);
            $.ajax({
                type: "POST",
                url: msg.d,
                data: {},
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (msg) {
                    //window.open(msg.d);
                }
            });
        }
    });
public void ProcessRequest(HttpContext context)
    {

        string FullFilePath = context.Server.MapPath("~/Certificates/" + "ExcelFile.xls");
        System.IO.FileInfo file = new System.IO.FileInfo(FullFilePath);
        if (file.Exists)
        {
            //For more MIME types list http://msdn.microsoft.com/en-us/library/ms775147%28VS.85%29.aspx
            context.Response.ContentType = MIMETypeUtility.MIMETypeDescription(MIMEType.Excel);
            context.Response.AddHeader("Content-Disposition", "attachment; filename='"" + file.Name + "'"");
            context.Response.AddHeader("Content-Length", file.Length.ToString());
            context.Response.WriteFile(file.FullName);
            context.Response.Flush();
            context.Response.End();
        }

    }

当我调试应用程序调用正确处理程序,但文件下载弹出不来。我在页面上尝试过的相同代码(不是在弹出窗口上)它工作得很好。谁能告诉我为什么这在我的情况下不起作用?

非常感谢。Prashant

不能使用ajax保存文件。它将获取数据,但永远不会显示下载对话框。参见使用jQuery下载文件。更多信息。

我最近自己也有类似的需求,最终让javascript Excel下载函数在页面上动态创建一个表单(操作指向生成Excel文件的.ashx处理程序)。然后,该函数用包含.ashx处理程序所需的任何参数的隐藏输入填充表单,然后最后提交它。

基于我所做的一个例子:

 function ExportExcel() {
    var formFragment = document.createDocumentFragment();
    var form = document.createElement("form");
    $(form).attr("action", baseUrl + "/WebServices/ExtraInfoWebService.asmx/Urlhttphandler")
        .attr("method", "POST");
    var inputField = document.createElement("input");
    $(inputField).attr("type", "hidden")
        .attr("id", "someId")
        .attr("name", "someId")
        .val(3);
    form.appendChild(inputField);
    formFragment.appendChild(form);
    $("body").append(formFragment);
    $(form).submit();
};