Problem in downloading a PDF file with Response

  1. #1

    Problem in downloading a PDF file with Response

    I create w PDF file and I want user to be allowed to download it. PDF is correct, because it works when I save it with FileStream.
    But when I use following method
    // Save the document...
                MemoryStream stream = new MemoryStream();
                renderer.PdfDocument.Save(stream, false);
    
                byte[] biteArray = new byte[stream.Length];
                //Set pointer to the beginning of the stream
                stream.Position = 0;
    
                //Read the entire stream
                stream.Read(biteArray, 0, (int)stream.Length);
    
                Response.Clear();
                Response.ClearHeaders();
                //Send the file to the output stream
                Response.Buffer = true;
    
                Response.AddHeader("Content-Length", biteArray.Length.ToString());
                Response.AddHeader("Content-Disposition", "attachment; filename= " + Server.HtmlEncode("test.pdf"));
    
                //Set the output stream to the correct content type (PDF).
                Response.ContentType = "application/pdf";
    
                //Output the file
                Response.BinaryWrite(biteArray);
    
                //Flushing the Response to display the serialized data
                //to the client browser.
                Response.Flush();
                Response.End();
    I receive an Request Failure:
    Status Code: 200 Status Text: BADRESPONSE

    I have DirectEvents enabled in web.config
      <httpModules>
          <add name="DirectRequestModule" type="Ext.Net.DirectRequestModule, Ext.Net" />
        </httpModules>
    When I used exactly the same code in pure ASP .NET without ext, everything works fine.

    The method is invoked in button click
       <ext:Button ID="m_btGenerate" runat="server" Text="Generate">
                        <DirectEvents>
                            <Click OnEvent="m_btGenerate_Click" Before="if(#{PdfFormPanel}.getForm().isValid()){return true;}else{Ext.Msg.show({icon: Ext.MessageBox.ERROR, msg: 'Nie wybrano daty.', buttons:Ext.Msg.OK}); return false;}" >
                            <EventMask ShowMask="true" />
                            </Click>
                        </DirectEvents>
                    </ext:Button>
    Last edited by reiben; Dec 14, 2010 at 2:44 PM.
  2. #2
    Hi,

    When I used exactly the same code in pure ASP .NET without ext, everything works fine.
    In pure ASP.NET you use postback, try postback with Ext.Net and all will be work also

    If you need to download the file using ajax request then please set IsUpload="true" for Click direct event (and remove mask from the direct event because we cannot determine end of request when you return a file)
  3. #3
    How about this demo code:
    public static string FolderName = "c:/";
    private void button1_Click(object sender, EventArgs e)
    {
    string fileName = FolderName + "Sample.pdf";
    string fileNameAdd = FolderName + "New.pdf";

    REDocument doc = REFile.OpenDocumentFile(fileName, new PDFDecoder());//use PDFDecoder open a pdf file
    int pageCount = doc.GetPageCount();//get pdf's page count
    Debug.WriteLine("Page Count: " + pageCount);

    BasePage newPage = doc.CreateEmptyPage();//create a new page by REDocument

    doc.AddPage(newPage, 2);//add a new page to REDocument

    pageCount = doc.GetPageCount();//get new pdf's page count
    Debug.WriteLine("Page Count: " + pageCount);

    REFile.SaveDocumentFile(doc, fileNameAdd, new PDFEncoder());//save new pdf

Similar Threads

  1. Replies: 1
    Last Post: Nov 02, 2012, 4:07 AM
  2. [CLOSED] Downloading a file from a menu directEvent
    By feanor91 in forum 2.x Legacy Premium Help
    Replies: 2
    Last Post: Jun 28, 2012, 7:19 AM
  3. [CLOSED] BADRESPONSE downloading a file
    By vadym.f in forum 1.x Legacy Premium Help
    Replies: 5
    Last Post: Feb 24, 2012, 4:12 PM
  4. downloading PDF file
    By unaltro2 in forum 1.x Help
    Replies: 0
    Last Post: Mar 10, 2011, 5:37 PM
  5. [CLOSED] DirectEvent from webService downloading file
    By pil0t in forum 1.x Legacy Premium Help
    Replies: 1
    Last Post: May 04, 2010, 6:05 AM

Posting Permissions