admin管理员组

文章数量:1331597

I make function in Aspx page and call this function from java script , Now i want to download files through Java script. But Download Dialog Doesn't Open.....

Download.Aspx:

            string pid = Request.QueryString["Did"].ToString();

            DataTable dt;
            dt = mon.GetFilePath(Convert.ToInt64(pid));
            FilePath = dt.Rows[0]["FilePath"].ToString();
            FileName = dt.Rows[0]["FileName"].ToString();
            FilePath = System.Web.HttpContext.Current.Server.MapPath("~//" + FilePath +    "");

            Response.Clear();
            Response.ClearHeaders();
            Response.ContentType = "application/ms-excel";
            Response.AddHeader("content-disposition", "attachment; filename=" + FileName + "");
            Response.WriteFile(FilePath);
            Response.End();

Jquery:

function DownloadAttach(pid){

   $.ajax({ type: "POST",
            url: "http://localhost:1988/DownLoad.aspx?Did=" + pid,
            dataType: "xml",

            processData: true,
            //error: function(XMLHttpRequest, textStatus, errorThrown) { ajaxError(XMLHttpRequest, textStatus, errorThrown); },
            success: function(xml) {

              //ShowMsg("projSaveMsg", "Attachment Deleted.");
            }
        });        

}

I make function in Aspx page and call this function from java script , Now i want to download files through Java script. But Download Dialog Doesn't Open.....

Download.Aspx:

            string pid = Request.QueryString["Did"].ToString();

            DataTable dt;
            dt = mon.GetFilePath(Convert.ToInt64(pid));
            FilePath = dt.Rows[0]["FilePath"].ToString();
            FileName = dt.Rows[0]["FileName"].ToString();
            FilePath = System.Web.HttpContext.Current.Server.MapPath("~//" + FilePath +    "");

            Response.Clear();
            Response.ClearHeaders();
            Response.ContentType = "application/ms-excel";
            Response.AddHeader("content-disposition", "attachment; filename=" + FileName + "");
            Response.WriteFile(FilePath);
            Response.End();

Jquery:

function DownloadAttach(pid){

   $.ajax({ type: "POST",
            url: "http://localhost:1988/DownLoad.aspx?Did=" + pid,
            dataType: "xml",

            processData: true,
            //error: function(XMLHttpRequest, textStatus, errorThrown) { ajaxError(XMLHttpRequest, textStatus, errorThrown); },
            success: function(xml) {

              //ShowMsg("projSaveMsg", "Attachment Deleted.");
            }
        });        

}
Share Improve this question edited Aug 19, 2011 at 11:47 Daniel Hilgarth 174k40 gold badges344 silver badges449 bronze badges asked Aug 19, 2011 at 11:42 Mayur BoradMayur Borad 1,2959 silver badges23 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 6

You don't want to make an AJAX call for this - just redirect the browser:

function DownloadAttach(pid){
     window.location = "http://localhost:1988/DownLoad.aspx?Did=" + pid;
}

本文标签: jqueryHow To Open Download Dialog box in JavascriptStack Overflow