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;

}

 

posted @ 2021-10-22 10:53  Challenge-Yourself  阅读(151)  评论(0编辑  收藏  举报