How to use web handlers for PDF creation?

情到浓时终转凉″ 提交于 2019-12-05 05:11:16

问题


I have pretty less knowledge in web handlers. All I know is web handlers are used for creating some dynamic file creation purpose.

And also I know how to add the Web handlers.

But, I need to use web handlers in my ASP.NET project for PDF creation purpose.


回答1:


You can have a HTTP handler to serve out your PDFs like this:

public void ProcessRequest (HttpContext context) {

    // Get your file as byte[]
    string filename = "....file name.";
    byte[] data = get your PDF file content here;

    context.Response.Clear();
    context.Response.AddHeader("Pragma", "public");
    context.Response.AddHeader("Expires", "0");
    context.Response.AddHeader("Content-Type", ContentType);
    context.Response.AddHeader("Content-Disposition", string.Format("attachment; filename={0}", filename));
    context.Response.AddHeader("Content-Transfer-Encoding", "binary");
    context.Response.AddHeader("Content-Length", data.Length.ToString());
    context.Response.BinaryWrite(data);
    context.Response.End(); 

}



回答2:


Read What is an HttpHandler in ASP.NET and MSDN: HTTP Handlers and HTTP Modules Overview.

You do not need a handler to serve files for downloading over HTTP. You can also generate and return a file response in a WebForms page, as well as from ASP.NET MVC and WebAPI.

Depending on the technology you use, check out:

  • HTTP handler: ASP.NET add a httphandler to edit downloaded file name, Dynamically create JS file using ASP.net Handler, MSDN: Serving Dynamic Content with HTTP Handlers
  • WebForms page: .NET MVC FileResult equivalent in Web Forms
  • ASP.NET MVC: Download file of any type in Asp.Net MVC using FileResult?
  • WebAPI: Returning binary file from controller in ASP.NET Web API

Of course, any layer on top of a handler (as opposed to running your code from a handler directly) adds additional overhead (though it'll be minimal), where I suspect WebForms to be the heaviest. MVC and WebAPI are run through the MvcHandler.



来源:https://stackoverflow.com/questions/30075075/how-to-use-web-handlers-for-pdf-creation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!