自己在前一段时间做了一个下载文件的功能,为了以后方便自己和其他需要用到的人,自己记录下来。
这是一个下载的方法:
public ActionResult Material_Down(string filename)
{
string fileName = filename;//客户端保存的文件名
string filePath = Server.MapPath("/Content/Upload/ps/" + fileName + "");//文件的路径
FileInfo fileInfo = new FileInfo(filePath);//实例化一个文件对象。
Response.Clear();//清除
Response.ClearContent();
Response.ClearHeaders();
Response.AddHeader("Content-Disposition", "attachment;filename=" + fileName);
Response.AddHeader("Content-Length", fileInfo.Length.ToString());
Response.AddHeader("Content-Transfer-Encoding", "binary");
Response.ContentType = "application/octet-stream";
Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
Response.WriteFile(fileInfo.FullName);
Response.Flush();
Response.End();
return View("Index");
}
可能每个人的具体代码环境不一样,但是大致的思路是差不多的。