简体   繁体   English

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

[英]How To Open Download Dialog box in Javascript?

I make function in Aspx page and call this function from java script , Now i want to download files through Java script. 我在Aspx页面中创建函数并从java脚本调用此函数,现在我想通过Java脚本下载文件。 But Download Dialog Doesn't Open..... 但下载对话框无法打开.....

Download.Aspx: 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: 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.");
            }
        });        

}

You don't want to make an AJAX call for this - just redirect the browser: 你不想为此做一个AJAX调用 - 只需重定向浏览器:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM