Click here to Skip to main content
15,891,976 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hello,
Bellow is my code which convert the data table into PDF form. This can be download at browser site, But I want to save that pdf file in Project's folder.
there some additional code for this. I tried lots. But I cant. please Help.

public void ExportToPDF(string userAutoId)
    {
        //Get the data from database into datatable
        string uId = userAutoId.Substring(0, 8);
        DataTable dt = new DataTable();
        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString);
        con.Open();
        SqlDataAdapter da = new SqlDataAdapter("getContacts", con);
        da.SelectCommand.CommandType = CommandType.StoredProcedure;
        da.SelectCommand.Parameters.AddWithValue("@userAutoId", userAutoId);
        //cmd.Parameters.Add("@retValue", System.Data.SqlDbType.VarChar).Direction = System.Data.ParameterDirection.ReturnValue; 
        da.Fill(dt);
    
        //Create a dummy GridView
        GridView GridView1 = new GridView();
        GridView1.AllowPaging = false;
        GridView1.DataSource = dt;
        GridView1.DataBind();
    
        Response.ContentType = "application/pdf";
        Response.AddHeader("content-disposition",
            "attachment;filename=DataTable.pdf");
        Response.Cache.SetCacheability(HttpCacheability.NoCache);
        StringWriter sw = new StringWriter();
        HtmlTextWriter hw = new HtmlTextWriter(sw);
        GridView1.RenderControl(hw);
        StringReader sr = new StringReader(sw.ToString());
        Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
        HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
        PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
        pdfDoc.Open();
        htmlparser.Parse(sr);
        pdfDoc.Close();

/*here I ant to code for save pdf file in project folder*/
        
        string strPath = HostingEnvironment.ApplicationPhysicalPath + @"TempFiles/";
        string fileName =uId + "ContactList.pdf";
        
        //PdfWriter writer = PdfWriter.GetInstance(pdfDoc new FileStream(strPath + fileName, FileMode.Create));
        //File.WriteAllText(strPath + fileName, sr.ToString());
        
        Response.Clear();
        Response.Write("{\"success\":true,\"isSuccess\":true,\"fileName\":\"" + fileName + "\"}");

        //Response.Write(pdfDoc);
        //Response.End();
    }   


What I have tried:

I have to save pdf file in project's local folder..
Posted
Updated 30-Jan-17 20:25pm
v2

Try Server.MapPath()
C#
string fileName =uId + "ContactList.pdf";
string strPath = Server.MapPath(@"TempFiles/"+fileName);

Reference: HttpServerUtility.MapPath Method (String) (System.Web)[^]
Hope, it helps :)
 
Share this answer
 
v2
Try This
public string CommonFileSave(HttpPostedFileBase postedFile, string filePath)
       {
           string resultResponse = "sccuess";

           if (!Directory.Exists(filePath))
           {
               Directory.CreateDirectory(filePath);
               postedFile.SaveAs(filePath + postedFile.FileName);
           }
           else
           {
               filePath = filePath + postedFile.FileName;
               if (!System.IO.File.Exists(filePath))
               {
                   postedFile.SaveAs(filePath);
               }
           }
           return resultResponse;
       }
 
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