如何在Javascript中打开下载对话框?

时间:2011-08-19 11:42:07

标签: javascript jquery .net

我在Aspx页面中创建函数并从java脚本调用此函数,现在我想通过Java脚本下载文件。但下载对话框无法打开.....

Download.Aspx:

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

            DataTable dt;
            dt = common.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.");
            }
        });        

}

1 个答案:

答案 0 :(得分:6)

您不想为此进行AJAX调用 - 只需重定向浏览器:

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