mvc只是一种模式,大家不要被这个英文吓唬了!
其实下载文件跟asp.net web form的一模一样的:
public ActionResult File(){
string fileName = "test.exe";//客户端保存的文件名
string filePath = Server.MapPath("/DownLoad/test.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();//可以直接返回空的动作,也可以redirect("your url")
}