What is Server.MapPath in ASP.Net

Last Reply 3 months ago By dharmendr

Posted 3 months ago

Can you explain where is filePath in your code?

byte[] bytes = File.ReadAllBytes(Server.MapPath(filePath));

 

protected void Save(object sender, EventArgs e)
{
    using (FileStream stream = new FileStream(Server.MapPath("~/Files/") + "ImageExport.pdf", FileMode.Create))
    {
        String[] texts = { chkAgree.Text };
        string filePath = "~/Files/" + Path.GetFileName("E-Sign.png");
        byte[] bytes = File.ReadAllBytes(Server.MapPath(filePath));
        Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 10f);
        PdfWriter writer = PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
        PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
        PdfWriter.GetInstance(pdfDoc, stream);
        pdfDoc.Open();
        string FONT = "c:/windows/fonts/WINGDING.TTF";
        string checkBox = "\u00fe";
        string uncheckBox = "o";
        BaseFont bf = BaseFont.CreateFont(FONT, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
        iTextSharp.text.Font f = new iTextSharp.text.Font(bf, 12);
        Paragraph ptrue = new Paragraph(checkBox, f);
        Paragraph pfalse = new Paragraph(uncheckBox, f);
        if (chkAgree.Checked)
        {
            pdfDoc.Add(ptrue);
        }
        else
        {
            pdfDoc.Add(pfalse);
        }   
        pdfDoc.Add(new Paragraph(chkAgree.Text));
        iTextSharp.text.Image img = iTextSharp.text.Image.GetInstance(bytes);
        pdfDoc.Add(img);
        iTextSharp.text.html.simpleparser.HTMLWorker hw = new iTextSharp.text.html.simpleparser.HTMLWorker(pdfDoc);
        hw.Parse(new StringReader(pnlDetails.InnerText));
        pdfDoc.Close();
        stream.Close();
    }
}
Posted 3 months ago
KatieNgoc says:
 string filePath = "~/Files/" + Path.GetFileName("E-Sign.png");
        byte[] bytes = File.ReadAllBytes(Server.MapPath(filePath));

File E-Sign.png is inside projects Files folder.

Change above line with the below.

string filePath = "~/Files/E-Sign.png";
byte[] bytes = File.ReadAllBytes(Server.MapPath(filePath))

Server.MapPath specifies the relative or virtual path to map to a physical directory.

Server.MapPath("~") returns the physical path to the root of the application.