Click here to Skip to main content
15,894,540 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

Here I tried to create a pdf file from datatble for an mvc3 application,It creates Locally on c drive,But after publishing it is not creating on the c drive from our testwebsite

Here is my function.

C#
public JsonResult ExportToPdf()
        {
            string retval;
            DataTable dt = new DataTable();
            List<applicationvariables> _pvtList = new List<applicationvariables>();
            common obj_EditList = new common();
            dt = obj_EditList.getcardholders(0, "", "");

            try
            {
                Document document = new Document();
                string path = "c:\\";
                PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(path + "cardholderdetails.pdf", FileMode.Create));
                document.Open();
                //iTextSharp.text.Image img = iTextSharp.text.Image.GetInstance("c://ggi logo.bmp");
                //document.Add(img);
                iTextSharp.text.Font font5 = iTextSharp.text.FontFactory.GetFont(FontFactory.HELVETICA, 8);
                //float[] columnDefinitionSize = { 22F, 22F, 12F, 7.75F, 7.77F, 7.77F, 7.77F, 7.77F, 10.88F, 10.88F, 10.88F, 4.75F, 7.77F, 7.77F, 7.77F, 7.77F, 7.77F, 7.77F, 9F };

                PdfPTable table = new PdfPTable(dt.Columns.Count);
                PdfPRow row = null;
                float[] widths = new float[] { 4f, 4f, 4f, 4f, 4f, 4f, 4f, 4f, 4f, 4f, 4f };

                table.SetWidths(widths);

                table.WidthPercentage = 100;
                int iCol = 0;
                string colname = "";
                Phrase p1Header = new Phrase("CarHolder Details", FontFactory.GetFont("verdana", 8));
                PdfPCell cell = new PdfPCell(new Phrase(p1Header));

                table.AddCell(cell);
                ////table.AddCell(cell);
                cell.Colspan = dt.Columns.Count;

                //cell.Border = 0;

                //cell.HorizontalAlignment = 1;
                foreach (DataColumn c in dt.Columns)
                {

                    table.AddCell(new Phrase(c.ColumnName, font5));
                }

                //cell.BackgroundColor = new iTextSharp.text.Color(0xC0, 0xC0, 0xC0);


                foreach (DataRow r in dt.Rows)
                {
                    if (dt.Rows.Count > 0)
                    {
                        table.AddCell(new Phrase(r[0].ToString(), font5));
                        table.AddCell(new Phrase(r[1].ToString(), font5));
                        table.AddCell(new Phrase(r[2].ToString(), font5));
                        table.AddCell(new Phrase(r[3].ToString(), font5));
                        table.AddCell(new Phrase(r[0].ToString(), font5));
                        table.AddCell(new Phrase(r[1].ToString(), font5));
                        table.AddCell(new Phrase(r[2].ToString(), font5));
                        table.AddCell(new Phrase(r[3].ToString(), font5));
                        table.AddCell(new Phrase(r[1].ToString(), font5));
                        table.AddCell(new Phrase(r[2].ToString(), font5));
                        table.AddCell(new Phrase(r[3].ToString(), font5));
                    }


                } document.Add(table);
                //document.Open();
             document.Close();
                retval = "PDF file has been generated your C drive";
            }
            catch (Exception ex)
            {
                retval = ex.Message;
              
            }

            return Json(retval, JsonRequestBehavior.AllowGet);
        }


please Help me

Thanks & Regards,
soumya
Posted
Updated 27-Jun-13 4:45am
v2
Comments
[no name] 27-Jun-13 10:08am    
Well debug it. You probably don't have permission to write files to the root directory of the C drive on your web server.
ZurdoDev 27-Jun-13 10:46am    
Without even looking at the code either the path on the server does not exist or the permissions are not write. 99% of the time, it is permissions issue. You need to find out what account is running your app pool and then make sure it has permissions to create files where you want it to.
soumyaraj 28-Jun-13 2:20am    
Thank you ryan,
Please let me know the way to save the file in download folder as it won't raise the permission issue
Please let me know where I have to change the code
ZurdoDev 28-Jun-13 7:36am    
It's not a code change. Your code is executing as a certain user account which is defined in IIS, likely. So, on the server, you have to grant that user account permissions so that the code can save the file.

1 solution

give permission to C: drive..

Check you given permission to folder..

Means read n write permission
 
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