Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi I want to save a pdf document into a folder inside visual studio that I have created.
I can export the pdf file but i dont know how to save it into a folder inside my solution

this is my code;
protected void btnPDF_Click(object sender, EventArgs e)
   {
       string path = Server.MapPath("~/OrderReport");
       Response.ContentType = "application/pdf";
       Response.AddHeader("content-disposition", "attachment;filename=UserDetails.pdf");
       Response.Cache.SetCacheability(HttpCacheability.NoCache);
       StringWriter sw = new StringWriter();
       HtmlTextWriter hw = new HtmlTextWriter(sw);
       this.Page.RenderControl(hw);
       StringReader sr = new StringReader(sw.ToString());
       Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 100f, 0.0f);
       HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
       PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
       pdfDoc.Open();
       htmlparser.Parse(sr);

       pdfDoc.Close();
       Response.Write(pdfDoc);

       Response.End();
   }

Can someone help!
Posted
Updated 20-Sep-20 17:44pm
Comments
Jignesh Khant 31-May-13 4:56am    
Through File Upload Control?
El Dev 31-May-13 5:03am    
There is no way to automatically save it without uploading?
El Dev 31-May-13 5:12am    
What if I do not want to upload the file but to save it automatically while exporting it.
renish patel 31-May-13 5:38am    
so you have upload files with out file upload controls r8?
ErBhati 28-Jan-14 2:21am    
Hello Dev

I am facing same problem .if you find the solution plz paste the code

XML
<form id="form1" runat="server" enctype="multipart/form-data">
 <input type="file" id="myFile" name="myFile" />
 <asp:Button runat="server" ID="btnUpload" OnClick="btnUploadClick" Text="Upload" />
</form>

code behind..
C#
protected void btnUploadClick(object sender, EventArgs e)
{
    HttpPostedFile file = Request.Files["myFile"];
    if (file != null && file.ContentLength )
    {
        string fname = Path.GetFileName(file.FileName);
        file.SaveAs(Server.MapPath(Path.Combine("~/App_Data/", fname)));
    }
}

This can help you...
Thanks
Happy coding...
 
Share this answer
 
v2
Hi El Dev, you can save your File in Files folder using following way...

string filename = Path.GetFileName(fileUpload1.PostedFile.FileName); fileUpload1.SaveAs(Server.MapPath("Files/"+filename));

Thanks
 
Share this answer
 
v2
Hi...
C#
//Get Filename from fileupload control
                string filename = Path.GetFileName(fulpdf.PostedFile.FileName);
                //Save images into Images folder
                fulpdf.SaveAs(Server.MapPath("~/foldername/" + filename

//Open the database connection
                        con = new MySqlConnection(cs);
                        cmd = new MySqlCommand("Insert into tablename(FileName,FileUrl) values(@filename,@fileurl)", con);
                        //Passing parameters to query
                        cmd.Parameters.AddWithValue("@filename", filename);
                        cmd.Parameters.AddWithValue("@fileurl", "~/foldername/" + filename);
                        con.Open();
                        cmd.ExecuteNonQuery();
                        //Close dbconnection
                        con.Close();


In asp.net,thank u.
 
Share this answer
 
v2
protected void btnPDF_Click(object sender, EventArgs e)
{
string path = Server.MapPath("~/OrderReport");
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=UserDetails.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
this.Page.RenderControl(hw);
StringReader sr = new StringReader(sw.ToString());
Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 100f, 0.0f);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
htmlparser.Parse(sr);

pdfDoc.Close();
Response.Write(pdfDoc);

Response.End();
}
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900