本文介绍了文件下载弹出无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的ASP.NET Web窗体应用程序。我的要求是要显示excel文件下载popupbox当用户单击给定链路上(此链接是poppage不是aspx页面)。我有一个链接aspx页面。当用户点击,它会通过调用JS函数我们调用Web服务方法生成html的弹出式屏幕。

I am working on ASP.NET Web form application.My requirement is to show excel file download popupbox when user clicks on the given link (This link is on poppage not on aspx page.).I have a aspx page with link. When user clicks,it will call js function through that we call web service method to generate html for popup screen.

code

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

Web方法

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

当用户点击lnkViewProduct codeStatus(以上由Web方法创建)。我们调用JS函数 ExportExcel 它调用的处理方法来处理文件下载。

when user clicks on "lnkViewProductCodeStatus " (above created by web method).we call JS function ExportExcel which calls the handler method to process file to download.

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但文件下载弹出是不是coming.same codeI尝试网页上(而不是弹出),它的工作原理fine.can有人指导我这是为什么不工作在我的情况。

when I debug application call goes correctly to hanlder but file download popup is not coming.same code i tried on page ( not on popup) it works fine.can anyone guide me why this is not working in my case.

非常感谢,PRASHANT

Thanks a lot,Prashant

推荐答案

这是不可能保存使用AJAX的文件。它会得到数据,但绝不会显示在下载对话框。请参见如何通过jquery.ajax 下载文件更信息。

It's not possible to save a file using ajax. It will get the data but the download dialog will never be shown. See How to download a file by jquery.ajax for more information.

我最近也有类似的要求自己,最后不得不JavaScript的Excel的下载功能动态地创建页面上的表单(与动作指向ashx的处理程序生成Excel文件)。然后,该函数填充表单包含由ashx的处理程序所需的任何参数隐藏的输入,最后提交了。

I recently had a similar requirement myself and ended up having the javascript Excel download function create a form on the page dynamically (with the action pointing to the .ashx handler that generates the Excel file). The function then populated the form with hidden inputs containing any parameters required by the .ashx handler and then finally submitting it.

这是例如基于我所做的:

An example based on what I did:

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

这篇关于文件下载弹出无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 23:26