mvc文件下载

时间:2024-01-08 10:39:56

public ActionResult xiazai(int id)
{
DataTable dt = bll.chaxun(id);
//获取文件名字
var filename = dt.Rows[0]["SCName"];
//完整路径
var newname = Server.MapPath("/EXCEL/" + filename);
//文件流
FileStream fs=new FileStream(newname.ToString(),FileMode.Open);
//数组
byte[] bytes = new byte[(int)fs.Length];
fs.Read(bytes, 0, bytes.Length);
fs.Close();
Response.Charset = "UTF-8";
Response.ContentType = "application/octet-stream";
Response.ContentEncoding = Encoding.Default;
Response.AddHeader("Content-Disposition", "attachment;filename=" + Server.UrlEncode(filename.ToString()));
Response.BinaryWrite(bytes);
Response.Flush();
Response.End();

return View();
}