Click here to Skip to main content
14,453,371 members
Rate this:
Please Sign up or sign in to vote.
I want to save the pdf file. I do not want to download that file although I want to save that file inro database. below code is pdf creation code now how can I save that pdf file into the database. I do not want to upload that file with file uploader actually I want to save that file directly in database.

What I have tried:

string attachment = "attachment; filename=" + "abc" + ".pdf";
                Response.ClearContent();
                Response.AddHeader("content-disposition", attachment);
                Response.ContentType = "application/pdf";
                StringWriter s_tw = new StringWriter();
                HtmlTextWriter h_textw = new HtmlTextWriter(s_tw);
                h_textw.AddStyleAttribute("font-size", "7pt");
                h_textw.AddStyleAttribute("color", "Black");
                Panel1.RenderControl(h_textw);//Name of the Panel
                Document doc = new Document();
                doc = new Document(PageSize.A4, 5, 5, 15, 5);
                FontFactory.GetFont("Times New Roman", 14);
                PdfWriter.GetInstance(doc, Response.OutputStream);
                doc.Open();
                StringReader s_tr = new StringReader(s_tw.ToString());
                HTMLWorker html_worker = new HTMLWorker(doc);
                html_worker.Parse(s_tr);
                doc.Close();
                Response.Write(doc);
Posted
Updated 29-Oct-19 2:15am
v2
Rate this:
Please Sign up or sign in to vote.

Solution 1

   
v2
Comments
Member 14192879 29-Oct-19 7:15am
   
I used the below code to save pdf. but it saves the pdf but the size of the file is very less. and when I unable to open file due to small size
Rate this:
Please Sign up or sign in to vote.

Solution 3

string contentType = Response.ContentType;

using (Stream fs = Request.InputStream)
{
using (BinaryReader br = new BinaryReader(fs))
{
//byte[] bytes = br.ReadBytes(fs.pdfDoc);
byte[] bytes = br.ReadBytes((Int32)fs.Length);
using (MySqlConnection con1 = new MySqlConnection("conectionstring"))
{
string query1 = "insert into tblFiles(clientid1,username,Name,ContentType,Data) values (@clientid1,@username,@Name, @ContentType, @Data)";
using (MySqlCommand cmd = new MySqlCommand(query1))
{
cmd.Connection = con;
cmd.Parameters.AddWithValue("@clientid1", TextBox1.Text);
cmd.Parameters.AddWithValue("@username", Session["username"].ToString());
cmd.Parameters.AddWithValue("@Name", filename);
cmd.Parameters.AddWithValue("@ContentType", contentType);
cmd.Parameters.AddWithValue("@Data", pdfDoc);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
//}
//}

//Response.Redirect(Request.Url.AbsoluteUri);
}
}
}
   
Comments
RickZeeland 29-Oct-19 7:26am
   
I think you need to use: cmd.Parameters.AddWithValue("@Data", bytes);
Richard Deeming 29-Oct-19 8:41am
   
If you want to update your question, click the green "Improve question" link and edit your question.

DO NOT post your update as a "solution".

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100