Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 677
  • Last Modified:

How to save a document to database using MemoryStream or Response.OutputStream?

hi all,
i have a gridview, when i select one row and click the button A, it will generate a PDF document, save it to database and popup/display it to users.  how to achieve it?

thanks a lot
viola
Response.ContentType = "application/pdf";
        Response.AddHeader("Content-Disposition", "inline;filename=EmailConfirmation.pdf");
        MemoryStream stream = new MemoryStream();
        InitPDF(stream ); 
foreach (GridViewRow row in gvEmail.Rows)
        {
            CheckBox cb = (CheckBox)row.FindControl("cbLetter");
            if (cb.Checked)
            {
                AddPage(row); //generate PDF file
            }
        }
        stream.WriteTo(Response.OutputStream); //if i use this method, it pops up a window to ask me to save the file. but i don't want to do this, i want to directly pops up the file to display it to user. how to do that?
        document.Close();
        
        Response.End();
}
 
private void InitPDF(Stream stream)
    {
        FontFactory.RegisterDirectories();
        Arial_8 = FontFactory.GetFont("Arial", 8, Font.NORMAL);
        Arial_9_bold = FontFactory.GetFont("Arial", 9, Font.BOLD);
        Arial_12 = FontFactory.GetFont("Arial", 12, Font.NORMAL);
        Arial_12_bold = FontFactory.GetFont("Arial", 12, Font.BOLD);
        Arial_12_underline = FontFactory.GetFont("Arial", 12, Font.UNDERLINE);
        document = new Document(PageSize.A4, 60, 60, 60, 90);
 
        writer = PdfWriter.GetInstance(document, stream);
        writer.PageEvent = new PDFTemplate(document, true, false, false);
        writer.SetEncryption(true, null, "aaa", PdfWriter.AllowPrinting);	// disable copy
 
        document.AddAuthor("tester");
        document.AddCreator("");
        document.AddSubject("");
        document.AddTitle("Email Confirmation");
 
        document.Open();
    }

Open in new window

0
viola123
Asked:
viola123
  • 2
1 Solution
 
Anurag ThakurTechnical ManagerCommented:
hi regarding the PDF popping up the dialog see the following links
http://www.velocityreviews.com/forums/t117955-file-open-and-save-dialog-poping-up-while-opening-pdf.html
http://bytes.com/forum/thread331070.html

saving the pdf file to sql server check the follwoing links
the link shows how to save an image file to the sql server but the same can be converted to pdf too
http://www.codeproject.com/KB/web-image/PicManager.aspx
0
 
viola123Author Commented:
hi,
i tried the solutions, they do not work for me. the file download window is still be popped up to ask you to save the PDF file.
0
 
viola123Author Commented:
Hi,
now i have to create two documents, one is used to save to database(this one using MemoryStream), the other one is used to pop up(this one using Response.OutputStream). it works quite well.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now