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

How To Open Download Dialog box in Javascript?

本文关键字:下载 对话框 Javascript      更新时间:2023-09-26

我在Aspx页面中创建了一个函数,并从java脚本中调用这个函数,现在我想通过java脚本下载文件。但是下载对话框打不开.....

下载。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.");
            }
        });        
}

您不希望对此进行AJAX调用-只需重定向浏览器:

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