日期:2014-05-17  浏览次数:20580 次

MVC3中如何实现文件下载?
如题,点击按钮就下载一个文件。跪求指教
建库建表建视图模型的就不必了,来个简单点的例子就行了

------解决方案--------------------
http://q.cnblogs.com/q/8061/
------解决方案--------------------
public ActionResult File(){
string fileName = "QQ五笔.exe";//客户端保存的文件名  
  string filePath = Server.MapPath("../../DownLoad/QQWubi_Setup.exe");//路径  

  FileStream fs = new FileStream(filePath, FileMode.Open);
  byte[] bytes = new byte[(int)fs.Length];
  fs.Read(bytes, 0, bytes.Length);
  fs.Close();
  Response.Charset = "UTF-8";
  Response.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8");
  Response.ContentType = "application/octet-stream";
    
  Response.AddHeader("Content-Disposition", "attachment; filename="+ fileName);
  Response.BinaryWrite(bytes);
  Response.Flush();
  Response.End();
return new EmptyResult();
}