2010-11-03 28 views
6

Sto utilizzando FileResult come valore di ritorno per una funzione in MVC che restituisce un file PDF..NET MVC FileResult equivalente in Web Form

Quale tipo di reso devo utilizzare nei Web Form?

Grazie

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; 
    } 

risposta

5

in ASP.NET Webforms è necessario scrivere il file al flusso di risposta manualmente. Non vi è alcuna astrazione dei risultati nei webform.

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(); 

Questo codice non è testato, ma questo dovrebbe portarti nella direzione generale.

3

Classico ASP.NET non ha l'idea di un tipo di ritorno . Il modo per avvicinarsi a questo sarebbe creare una pagina/gestore .ashx personalizzata per servire il file.

Il codice dietro per questo file dovrebbe essere qualcosa di simile a:

public class Download : IHttpHandler 
{ 
    public void ProcessRequest (HttpContext context) 
    { 
     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 
      Response.ContentType = ""text/pdf""; 
      Response.AppendHeader("Content-Disposition","attachment; filename=File.pdf"); 

      outputDoc.Save(Response.OutputStream, true); 

      gfxs.Dispose(); 
      pdfPage.Close(); 
     } 
     finally 
     { 
      outputDoc.Close(); 
      outputDoc.Dispose(); 
     } 
    } 

    public bool IsReusable 
    { 
     get 
     { 
       return false; 
     } 
    } 
} 
+0

Grazie per "Content-Disposition" intestazione –