2010-11-03 16 views
6

में .NET एमवीसी फ़ाइल रीसेट समकक्ष मैं एमवीसी में एक फ़ंक्शन के लिए रिटर्न वैल्यू के रूप में FileResult का उपयोग कर रहा हूं जो एक पीडीएफ फ़ाइल देता है।वेब प्रपत्र

वेब फॉर्म में मुझे किस प्रकार का उपयोग करना चाहिए?

धन्यवाद

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

उत्तर

5

ASP.NET वेबफ़ॉर्म में आप प्रतिक्रिया धारा करने के लिए फ़ाइल को मैन्युअल रूप से लिखने के लिए की आवश्यकता होगी। वेबफॉर्म में कोई परिणाम नहीं है।

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

यह कोड परीक्षण नहीं किया गया है, लेकिन यह आपको सामान्य दिशा में ले जाना चाहिए।

3

क्लासिक एएसपी.नेट में रिटर्न प्रकार का विचार नहीं है। इस तक पहुंचने का तरीका फाइल को पूरा करने के लिए एक कस्टम .ashx पेज/हैंडलर बनाना होगा।

के पीछे के लिए इस फ़ाइल को कुछ इसी तरह दिखना चाहिए आपका कोड: के लिए "सामग्री-विन्यास" शीर्षक

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

धन्यवाद –

संबंधित मुद्दे