本文介绍了如何通过代码访问浏览器上的Pdf文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的pdf文件位于服务器xx.xx.xx.12中,我从本地主机运行我的Web应用程序而不发布它。我使用UNC路径连接到不同的服务器。现在我想显示位于以下路径中的pdf文件.. \\xx.xx.xx.12\GeneratedPDF\031714LR9FC842DB1.pdf。在浏览器上输入路径时,pdf文件被打开,但是当我试图通过代码打开它时...我收到一个错误::代码是

My pdf file is in server xx.xx.xx.12 and I am running my web application from local host with out publishing it.. I am using UNC path to connect to different servers. now I wanted to show pdf file located in following path.. \\xx.xx.xx.12\GeneratedPDF\031714LR9FC842DB1.pdf . on entering the path on browser, pdf file is opened but when i tried to open it via code.. i got an error:: the code is

string pdfFileName = Convert.ToString(Session["pdfFileName"]);
      string fullPathOfPdfFile = string.Empty;
      fullPathOfPdfFile = @"\\xx.xx.xx.12\GeneratedPDF\" + pdfFileName;
      if (pdfFileName != null)
      {
          try
          {

              Response.ContentType = "application/pdf";
              Response.TransmitFile(fullPathOfPdfFile);
              Session.Remove("pdfFileName");
          }
          catch (Exception ex)
          {
      string message = ex.message;
          }
      }







抛出异常登录失败:未知用户名或密码错误。我在这个问题上挣扎了3天..任何人都可以帮助我...




an exception is thrown " Logon failure: unknown user name or bad password. " . i am struggling on this issue for 3 days.. can anyone help me...

推荐答案


try
           {
               Response.Clear();
               Response.ClearContent();
               //
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=" + this.FileName);
               //
Response.Cache.SetCacheability(System.Web.HttpCacheability.Public);
               string filePath = string.Format("{0}\\{1}", Server.MapPath(this.VirtualPath), this.FileName));
               if (System.IO.File.Exists(filePath))
               {
                   Response.TransmitFile(filePath);
               }
               else
               {
                   Response.Write("File Not Found!");
               }
               //
               Response.End();
           }
           catch (Exception ex)
           {
            //...TO DO!   
           }





在你的Web.config中你应该有下一个可以访问的设置 PDF文件夹命名内容给所有用户:





An in your Web.config you should have the next setting that will make accessible the "PDF folder" named "Content" to all users:

<location path="Content">
    <system.web>
      <authorization>
        <allow users="*" />
      </authorization>
    </system.web>
  </location>


这篇关于如何通过代码访问浏览器上的Pdf文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 20:39