function downLoadFile(){
var src = "DownLoad.ashx?Method=DownLoadFile&FileName=测试";
downLoad(src);
}
function downLoad(src) {
if (typeof (downLoad.iframe) == "undefined") {
var iframe = document.createElement("iframe");
downLoad.iframe = iframe;
document.body.appendChild(downLoad.iframe);
}
downLoad.iframe.src = src;
downLoad.iframe.style.display = "none";
}
DownLoad.ashx:
public void ProcessRequest(HttpContext context)
{
string method = Request["Method"] ?? "";
if (!string.IsNullOrEmpty(method))
{
foreach (MethodInfo mi in this.GetType().GetMethods())
{
if (mi.Name.ToLower() == method.ToLower())
{
mi.Invoke(this, null);
break;
}
}
}
}
public void DownLoadFile()
{
string strFileName = Request["FileName"];
string strTempSavePath = string.Format("{0}/{1}.Doc", Server.Map("~/Doc"), "Test");
FileInfo DownloadFile = new FileInfo(strTempSavePath);
Response.Clear();
Response.ClearHeaders();
Response.Buffer = false;
Response.ContentType = "application/octet-stream";
Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(string.Format("{0}.doc", strFileName), System.Text.Encoding.UTF8));
Response.AppendHeader("Content-Length", DownloadFile.Length.ToString());
Response.WriteFile(DownloadFile.FullName);
Response.Flush();
Response.End();
}
浙公网安备 33010602011771号