我创建了一个文件夹(文件),我希望在单击按钮(btnGenerate_Click)之后,它应该创建一个pdf文件并将其保存到文件夹中,命名为"文件"。但我经常收到错误消息:"无法找到路径的一部分" C:\ User \ VS \ Intra \ Intra.Admin \ File \"但路径的位置是正确的....
protected void btnGenerate_Click(object sender, EventArgs e)
{
string FilePath = MapPath("~/File/"); //here!
iTextSharp.text.Document pdfDoc = new iTextSharp.text.Document(iTextSharp.text.PageSize.A4, 20f, 20f, 20f, 20f);
PdfWriter.GetInstance(pdfDoc, new FileStream(FilePath, FileMode.Create));
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
GridView1.AllowPaging = false;
GridView1.HeaderRow.Cells[1].Text = "Message";
GridView1.HeaderRow.Font.Bold = true;
GridView1.RenderControl(hw);
StringReader sr = new StringReader(sw.ToString());
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();
Response.Write(pdfDoc);
Response.ContentType = "Application/pdf";
Response.WriteFile(FilePath);
Response.End();
}

答案 0 :(得分:0)
FilePath是一个目录。你应该附加一个文件名,如下所示:
string FilePath = MapPath("~/File/" + fileName);