#### 只适合处理小文件,文件太大会撑爆内存
public FileResult DownLoadFile(string key = "", string fileName = "")
{
string filePath = GetObjectFromCache<string>(key) as string;
if (!string.IsNullOrEmpty(filePath))
{
FileStream stream = new FileInfo(filePath).OpenRead();
Byte[] buffer = new Byte[stream.Length];
////下载后将Excel立即删除
//从流中读取字节块并将该数据写入给定缓冲区buffer中
stream.Read(buffer, 0, Convert.ToInt32(stream.Length));
stream.Close();
if (System.IO.File.Exists(filePath))
{
System.IO.File.Delete(filePath);
}
MemoryStream m = new MemoryStream(buffer);
if (m!=null)
{
return File(m, "application/octet-stream", fileName);
}
}
return null;
}
网友评论