从字节下载PDF文件[] [英] PDF file download from byte[]

查看:63
本文介绍了从字节下载PDF文件[]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在努力从ASP.Net MVC C#中的bytes[]中下载PDF文件.下面的代码工作正常.

I've been working to do a PDF file download from bytes[] in ASP.Net MVC C#. The below code is working fine.

对于相同的PDF下载过程,我需要将代码转换为.NET Core.

I need to convert the code to .NET Core for the same PDF download process.

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)行的响应. 预先谢谢.!

I'm tried to convert the above code to .NET Core. I get an error: OutputStream method doesn't contain Response for this ms.WriteTo(Response.OutputStream) line. Thanks In advance.!

推荐答案

@Luaan先生已经给出了答案,但这可能是我的代码也对您有所帮助,所以我与您分享.

@Luaan sir already gave answer, but it may be my code also help you, so I share it.

从文件夹下载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核心将PDF返回到浏览器

For more info please also see this question and answer. Return PDF to the Browser using Asp.net core

这篇关于从字节下载PDF文件[]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆