JavaScript-文件下载弹出窗口不起作用

我正在研究ASP.NET Web表单应用程序.
我的要求是当用户单击给定链接时显示excel文件下载弹出框(此链接位于poppage而不是aspx页面上.).
我有一个带链接的aspx页面.当用户单击时,它将通过我们调用Web服务方法来调用js函数,以生成用于弹出屏幕的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
    });
}

网络方法:

[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,该函数调用处理程序方法来处理要下载的文件.

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();
        }



    }

当我调试应用程序调用正确转到hanlder,但文件下载弹出窗口没有到来时.我在页面上尝试过的相同代码(不在弹出窗口上)工作正常.任何人都可以指导我为什么这在我的情况下不起作用

非常感谢,
PRASHANT

解决方法:

无法使用ajax保存文件.它将获取数据,但将永远不会显示下载对话框.有关更多信息,请参见Download a file by jQuery.Ajax.

最近,我本人也有类似的要求,最终让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();
};
上一篇:深入理解asp.net里的HttpModule机制


下一篇:c#同时执行2个线程