Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.NET MVC FileResult equivalent in Web Forms

I'm using FileResult as a return value for a function in MVC that returns a PDF file.

What return type should I use in Web Forms?

Thanks

public FileResult PrintPDFVoucher(object sender, EventArgs e)
    {
        PdfDocument outputDoc = new PdfDocument();
        PdfDocument pdfDoc = PdfReader.Open(
            Server.MapPath(ConfigurationManager.AppSettings["Template"]),
            PdfDocumentOpenMode.Import
        );

        MemoryStream memory = new MemoryStream();

        try
        {
            //Add pages to the import document
            int pageCount = pdfDoc.PageCount;
            for (int i = 0; i < pageCount; i++)
            {
                PdfPage page = pdfDoc.Pages[i];
                outputDoc.AddPage(page);
            }
            //Target specifix page
            PdfPage pdfPage = outputDoc.Pages[0];

            XGraphics gfxs = XGraphics.FromPdfPage(pdfPage);
            XFont bodyFont = new XFont("Arial", 10, XFontStyle.Regular);


            //Save 
            outputDoc.Save(memory, true);
            gfxs.Dispose();
            pdfPage.Close();
        }
        finally
        {
            outputDoc.Close();
            outputDoc.Dispose();
        }

        var result = new FileContentResult(memory.GetBuffer(), "text/pdf");
        result.FileDownloadName = "file.pdf";
        return result;
    }
like image 987
ausesr Avatar asked Nov 03 '10 15:11

ausesr


People also ask

Which is better Web Forms or MVC?

Asp.Net Web Form has built-in data controls and best for rapid development with powerful data access. Asp.Net MVC is lightweight, provide full control over markup and support many features that allow fast & agile development. Hence it is best for developing an interactive web application with the latest web standards.

Can we use Web Forms in MVC?

Luckily, the answer is yes. Combining ASP.NET Webforms and ASP.NET MVC in one application is possible—in fact, it is quite easy. The reason for this is that the ASP.NET MVC framework has been built on top of ASP.NET.

Is MVC faster than Web Forms?

If you want to have a faster development cycle than Web Forms might be the best option. If time, money, and energy to develop an application from the scratch is not a constraint then MVC could potentially be the better option.

What is difference between MVC and Web Forms?

The main difference between Webform and MVC is that the Webform follows a traditional event-driven development model while the MVC follows a Model, View, and, Controller pattern based development model. ASP.NET is a web framework developed by Microsoft.


1 Answers

In ASP.NET Webforms you'll need to write the file to the Response stream manually. There is no result abstraction in webforms.

  Response.ContentType = "Application/pdf";      
  //Write the generated file directly to the response stream
  Response.BinaryWrite(memory);//Response.WriteFile(FilePath); if you have a physical file you want them to download
  Response.End();

This code is not tested, but this should get you in the general direction.

like image 142
Jab Avatar answered Sep 30 '22 06:09

Jab