我在 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;
}
关于javascript - 如何在 Javascript 中打开下载对话框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7121027/