o任飘零o

别人给了你一滴水,你给了别人些什么?
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

asp.net 文件下载

Posted on 2016-03-16 13:26  o任飘零o  阅读(113)  评论(0)    收藏  举报

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();

}