mvc文件下载

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();
}

上一篇:一个读取C#特性Description方法


下一篇:JMeter:响应结果乱码解决方法