MVC资源下载

public ActionResult DownLoadFile(string type, string fileName)
{

//string filePath = Server.MapPath("File/") + Session["file"].ToString();//要下载的资源在本机

string filePath =@"\\192.168.1.90\wwwroot\img.cn-school.com\images\667027ee9425480e8881fd923158317d.png";//要下载的资源在某个服务器
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();
}

posted on 2013-03-15 17:29  朝着  阅读(211)  评论(0编辑  收藏  举报