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;
}
标签:Read,string,stream,显示文件,C#,bytes,fileStream,MVC,new 来源: https://www.cnblogs.com/lbliubinlb/p/15437751.html