1. 客户端
发送Get请求某个Action,将filePath
作为查询参数传递
例如:
location.href='//www.greatytc.com/Content/DownloadFile?filePath='+filePath;
或者<a href="//www.greatytc.com/Content/DownloadFile?filePath=... ">fileName</a>
2. 服务器端
public ActionResult DownloadFile(string filePath)
{
FileInfo info = new FileInfo(filePath);
long fileSize = info.Length;
Response.Clear();
Response.ContentType = "application/x-zip-compressed";
Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(Path.GetFileName(filePath)));
Response.AddHeader("Content-Length", fileSize.ToString());
Response.TransmitFile(filePath, 0, fileSize);
Response.Flush();
return null;
}
注:
在Response Header中加入的“filename=
”,用来指定下载到客户端的默认文件名。