簡體   English   中英

如何在Javascript中打開下載對話框?

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

我在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.");
            }
        });        

}

你不想為此做一個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