【问题标题】:Relative path in server.mappathserver.mappath 中的相对路径
【发布时间】:2014-02-01 18:03:55
【问题描述】:

我正在使用 iTextSharp 从文件路径读取 pdf 文件,编辑并在浏览器中打开文件。

当我在本地运行以下代码时,它可以正常工作。发布并托管后,我无法从文件路径读取文件。我想我在相对路径上犯了一些错误。

 string path = System.Web.HttpContext.Current.Server.MapPath("~/Doc/template.pdf");

请帮帮我。

using iTextSharp.text.pdf;


     private void GeneratePDF()
     {

         byte[] bytes = null;



         using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
         {

            string path = System.Web.HttpContext.Current.Server.MapPath("~/Doc/template.pdf");
             // Create the form filler
             using (PdfReader pdfReader = new PdfReader(path))
             {
                 using ( PdfStamper pdfStamper = new PdfStamper(pdfReader, ms))
                 {
                     StringBuilder sbstring = null;
                     // Get the form fields
                     AcroFields testForm = pdfStamper.AcroFields;
                    ....
                    ....

                     PdfContentByte overContent = pdfStamper.GetOverContent(1);
                     pdfStamper.FormFlattening = true;
                 }
             }
             bytes = ms.ToArray(); 
         }

         //Writes it back to the client
         Response.Clear();
         Response.ContentType = "application/pdf";
         Response.AddHeader("content-disposition", "attachment;  filename=" + 
         sbFileName.ToString() + "");
         Response.BinaryWrite(bytes);
     }
}

【问题讨论】:

  • 您是否收到错误消息?

标签: path itextsharp relative-path filepath


【解决方案1】:

相对路径正确。我检查了日志和部署的文件夹。 pdf 文件未包含在包中。完成后,它工作得很好。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-01-05
    • 1970-01-01
    • 2011-06-29
    • 2011-06-15
    • 2011-02-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多