我一直在努力做一个PDF
文件下载自bytes[]
在 ASP.Net MVC C# 中。下面的代码工作正常。
我需要将代码转换为.NET Core
对于相同的PDF
下载过程。
string fileName = "testFile.pdf";
byte[] pdfasBytes = Encoding.ASCII.GetBytes(fileBytes); // Here the fileBytes are already encoded (Encrypt) value. Just convert from string to byte
Response.Clear();
MemoryStream ms = new MemoryStream(pdfasBytes);
Response.ContentType = "application/pdf";
Response.Headers.Add("content-disposition", "attachment;filename=" + fileName);
Response.Buffer = true;
ms.WriteTo(Response.OutputStream);
Response.Flush();
Response.End();
我尝试将上面的代码转换为.NET Core
。我收到错误:输出流方法不包含此响应ms.WriteTo(Response.OutputStream)线。
提前致谢。!
@Luaan先生已经给出了答案,但可能我的代码也对你有帮助,所以我分享它。
从文件夹下载 PDF 文件
[HttpGet]
public FileStreamResult DownloadPdfFile(string fileName)
{
var stream = new FileStream(Directory.GetCurrentDirectory() + "\\wwwroot\\WriteReadData\\" + fileName, FileMode.Open);
return new FileStreamResult(stream, "application/pdf");
}
从数据库下载pdf文件
[HttpGet]
public FileStreamResult DownloadFileFromDataBase(string id)
{
var _fileUpload = _db.FileUpload.SingleOrDefault(aa => aa.fileid == id); // _fileUpload.FileContent type is byte
MemoryStream ms = new MemoryStream(_fileUpload.FileContent);
return new FileStreamResult(ms, "application/pdf");
}
有关更多信息,另请参阅此问题和答案。使用Asp.net core将PDF返回到浏览器 https://stackoverflow.com/questions/40486431/return-pdf-to-the-browser-using-asp-net-core
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)