简体   繁体   English

ABCpdf-使用.NET Core 2.1下载PDF-HttpContext / HttpResponse

[英]ABCpdf - Download PDF with .NET Core 2.1 - HttpContext/HttpResponse

I'm creating a web page that will allow the user to download a report as a PDF using ABCpdf. 我正在创建一个网页,允许用户使用ABCpdf将报告下载为PDF。 But reading the documentation, the only options I see are by using doc.Save("test.pdf") (which saves the file on the server that is hosting the application) or using 'HttpContext.Current.ApplicationInstance.CompleteRequest();' 但是阅读文档时,我看到的唯一选择是使用doc.Save("test.pdf") (将文件保存在托管应用程序的服务器上)或使用'HttpContext.Current.ApplicationInstance.CompleteRequest();' doc.Save("test.pdf") 'HttpContext.Current.ApplicationInstance.CompleteRequest();' (which saves on the client side, which is what I want, but HttpContext.Current is not available on .NET Core. (这是在客户端保存的,这是我想要的,但是HttpContext.Current在.NET Core上不可用。

The band-aid solution I have is with the doc.Save() , I would save the file on the server then send a link to the view which then downloads it from the server. 我拥有的创可贴解决方案是doc.Save() ,我将文件保存在服务器上,然后将链接发送到视图,然后从服务器下载该视图。 A potential risk I can think of is making sure to 'clean up' after the download has commenced on the server. 我可以想到的潜在风险是,确保在服务器上开始下载后进行“清理”。

Is there a alternative/.NET Core equivalent for HttpContext.Current and also HttpResponse? 是否有替代/.NET Core等效于HttpContext.Current和HttpResponse?

Here is the code that I'd like to make work: 这是我要工作的代码:

byte[] theData = doc.GetData();
Response.ClearHeaders();
Response.ClearContent();
Response.Expires = -1000;
Response.ContentType = "application/pdf";
Response.AddHeader("content-length", theData.Length.ToString());
Response.AddHeader("content-disposition", "attachment; filename=test.pdf"); 
Response.BinaryWrite(theData);
HttpContext.Current.ApplicationInstance.CompleteRequest();

Errors I get (non-verbose) 我收到的错误(不详细)

'HttpResponse' does not contain a definition for 'ClearHeaders'
'HttpResponse' does not contain a definition for 'ClearContent'
'HttpResponse' does not contain a definition for 'Expires'
'HttpResponse' does not contain a definition for 'AddHeader'
'HttpResponse' does not contain a definition for 'BinaryWrite'
'HttpContext' does not contain a definition for 'Current'

I've updated this answer to something that actually works! 我已将此答案更新为实际有效的方法! GetStream does what you need, however to facilitate a file download in .NET Core it would be far easier if you create a controller as described in https://docs.microsoft.com/en-us/aspnet/core/tutorials/first-web-api?view=aspnetcore-2.1 . GetStream可以满足您的需求,但是,如果要在.NET Core中方便地下载文件,则按照https://docs.microsoft.com/zh-cn/aspnet/core/tutorials/first中的说明创建控制器将容易得多。 -web-api?view = aspnetcore-2.1 Then you can create a route controller to serve the file from the stream as shown in Return PDF to the Browser using Asp.net core . 然后,您可以创建一个路由控制器,以使用Asp.net core将流中的文件提供服务,如将PDF返回到浏览器所示。 So your controller would look something like: 因此,您的控制器将类似于:

[Route("api/[controller]")]
public class PDFController : Controller {
// GET: api/<controller>
    [HttpGet]
    public IActionResult Get() {
        using (Doc theDoc = new Doc()) {
            theDoc.FontSize = 96;
            theDoc.AddText("Hello World");
            Response.Headers.Clear();
            Response.Headers.Add("content-disposition", "attachment; filename=test.pdf");
            return new FileStreamResult(theDoc.GetStream(), "application/pdf");
        }
    }
}

Out of curiosity I just mocked this up and it does work - serving the PDF direct to the browser as download when you go to the URL localhost:port/api/pdf . 出于好奇,我只是对此进行了模拟,它确实起作用了-当您转到URL localhost:port / api / pdf时,将PDF直接下载到浏览器中。 If you make the content-disposition "inline; filename=test.pdf" it will show in the browser and be downloadable as test.pdf. 如果将内容处置设置为“ inline; filename = test.pdf”,它将显示在浏览器中,并可以作为test.pdf下载。

More information on the GetStream method here: https://www.websupergoo.com/helppdfnet/default.htm?page=source%2F5-abcpdf%2Fdoc%2F1-methods%2Fgetstream.htm 有关GetStream方法的更多信息,请参见: https ://www.websupergoo.com/helppdfnet/default.htm?page = source%2F5-abcpdf%2Fdoc%2F1-methods%2Fgetstream.htm

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM