【发布时间】:2019-08-05 07:09:13
【问题描述】:
我一直致力于在 ASP.Net MVC C# 中从bytes[] 下载PDF 文件。下面的代码工作正常。
我需要将代码转换为.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。我收到一个错误:OutputStream 方法不包含此 ms.WriteTo(Response.OutputStream) 行的响应。
提前致谢!
【问题讨论】:
-
我很确定
byte[] pdfasBytes = Encoding.ASCII.GetBytes(fileBytes)没有正确加载(二进制!)PDF 文件的字节 -
“加密”是什么意思?如果文件以某种方式被加密,客户端会知道如何将其用作 PDF 文件吗?
-
@Lasse,filebytes 变量使用 GetStream() 方法存储编码值,该值将字符串变量传递给另一个函数。所以在这里我转换为字节[]。使用 .net mvc 中的上述代码成功下载了 pdf。这段代码我想转换 .net 核心。 Response.Outputstream 没有进入 .net 核心。
标签: c# asp.net-core-2.0