C# MVC从其他系统获取文件流,显示文件

public FileResult GetAuditPrintPdf(string bh,string url)

try

var client = new WebClient();
string tempFile = Path.GetTempFileName();
client.DownloadFile(url, tempFile);//下载临时文件
var stream = FileToStream(tempFile, true);
return new FileStreamResult(stream, "application/pdf");
}
catch (Exception ex)
{
throw new Exception("文件不存在");
}
}

public static Stream FileToStream(string fileName, bool isDelete = false)
{

FileStream fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read);

byte[] bytes = new byte[fileStream.Length];

fileStream.Read(bytes, 0, bytes.Length);

fileStream.Close();

Stream stream = new MemoryStream(bytes);
if (isDelete)
{
System.IO.File.Delete(fileName);
}
return stream;

}

 

上一篇:HMM 隐马尔可夫模型 MATLAB


下一篇:Azure基础:什么是Azure计算服务(7)