如何通过代码访问浏览器上的Pdf文件 [英] How Do I Access Pdf File On Browser Via Code

查看:95
本文介绍了如何通过代码访问浏览器上的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...

推荐答案

如果你能通过网络浏览器获取文件,那么你可以尝试WebRequest类从代码中获取文件。以下是使用WebRequest类的示例:



http://msdn.microsoft.com/en-us/library/456dfw4f(v = vs.110).aspx [ ^ ]
If you are able to get the file through a web browser, then you can try WebRequest class to fetch the file from code. Here is an example of using WebRequest class:

http://msdn.microsoft.com/en-us/library/456dfw4f(v=vs.110).aspx[^]


您应该尝试下一个示例:



You should try like in the next example:

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文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆