【问题标题】:How to open file with its application(the file not exist in the web server)?如何使用其应用程序打开文件(该文件在 Web 服务器中不存在)?
【发布时间】:2011-04-04 23:35:48
【问题描述】:

我想问一下,当单击特定链接按钮或超链接时,如何使用其应用程序打开特定文件(该文件在服务器之外,我有一个存储在配置文件中的相对路径)。 .

喜欢:

开场

.docx with word.

.pdf 与 acrobat 阅读器

我尝试了几种方法,但是,我得到了不同的错误,例如

不能使用前导 .. 退出顶级目录

我的 .cs:

public void ProcessRequest(HttpContext context)
        {
            
                int newsId = int.Parse(context.Session["newsId"].ToString());
                int FK_UnitId = int.Parse(context.Session["UserData"].ToString());
                string dirPathForTextFiles = ConfigurationManager.AppSettings.GetValues("ThePath").First() + "/" + "NewsTextFiles" + "/" + "UnitNum" + FK_UnitId.ToString() + "_" + "NewsNum" + newsId + "/";
                DataTable UpdatedDateTable = (DataTable)context.Session["theLastUpdatedTextFile"];
                UpdatedDateTable.AcceptChanges();
                context.Session.Add("currentTextFile", UpdatedDateTable);
                List<string> l = new List<string>(UpdatedDateTable.Rows.Count);
 
                try
                {

                    l.Add(dirPathForTextFiles + UpdatedDateTable.Rows[0]["fileName"].ToString());
                    context.Response.ContentType = getContentType(dirPathForTextFiles + UpdatedDateTable.Rows[0]["fileName"].ToString());
                    System.Diagnostics.Process.Start(l[0]);         
                    context.ClearError();

                }
                catch (IOException e)
                {
                    string message = e.Message;
                }

            
        }

        string getContentType(String path)
        {
            switch (Path.GetExtension(path))
            {
                case ".doc": return "   application/msword";
                case ".docx": return "application/msword";
                case ".pdf": return "application/pdf";
               
                default: break;
            }
            return "";
        }

【问题讨论】:

  • 你在问什么?你能举个例子吗?

标签: c# .net asp.net download


【解决方案1】:

为了获得服务器上的完整文件路径,您需要使用 Server.MapPath。

string fullFileName = Server.MapPath("../myFile.pdf");

编辑:之后您需要 Process 对象来“运行”它:

System.Diagnostics.Process.Start(fullFileName);

编辑 2:如果您希望在客户端打开文件,最好的办法是创建并HTTP Handler 并在您的响应中设置适当的mime type,然后再将其流出来自您的处理程序。

编辑 3:将文件流式传输到客户端的代码。

public void ProcessRequest(HttpContext context)  
{   
   int newsId = int.Parse(context.Session["newsId"].ToString());
   int FK_UnitId = int.Parse(context.Session["UserData"].ToString());
   string dirPathForTextFiles =  ConfigurationManager.AppSettings.GetValues("ThePath").First() + "/" + "NewsTextFiles" + "/" + "UnitNum" + FK_UnitId.ToString() + "_" + "NewsNum" + newsId + "/";
   DataTable UpdatedDateTable = (DataTable)context.Session["theLastUpdatedTextFile"];
   UpdatedDateTable.AcceptChanges();
   context.Session.Add("currentTextFile", UpdatedDateTable);
   List<string> l = new List<string>(UpdatedDateTable.Rows.Count);

   try
   {

      l.Add(dirPathForTextFiles + UpdatedDateTable.Rows[0]["fileName"].ToString());
       context.Response.ContentType = getContentType(dirPathForTextFiles + UpdatedDateTable.Rows[0]["fileName"].ToString());
       using (FileStream fs = new FileStream(l[0], FileMode.Open, FileAccess.Read))
       {
          long chunkSize = fs.Length; 
          byte[] buf = new byte[chunkSize]; 
          int bytesRead = 1; 
          bytesRead = fs.Read(buf, 0,(int)chunkSize); 
          if (bytesRead > 0) context.Response.OutputStream.Write(buf, 0, buf.Length);
          context.Response.OutputStream.Flush();
      }

  }
  catch (IOException e)
  {
     string message = e.Message;
  }   
}

【讨论】:

  • 我知道,但是文件来自我的服务器,我想用它的应用程序打开这个文件
  • 我编辑了一个可能的解决方案,但是你想在 SERVER 上打开它还是让用户在 CLIENT 上打开文件?
  • 我希望用户在客户端打开它
  • 好的,Edit 2 让您开始了解如何将文件流式传输到客户端。 Mime 类型对于允许用户打开适当的应用程序客户端非常重要。
  • 是的,我使用 HTTP 处理程序,但我想要的是(使用其应用程序打开文件的代码)
【解决方案2】:

System.Diagnostics.Process.Start("Start FilePath")

【讨论】:

  • 我收到以下错误“系统找不到指定的文件”虽然路径正确但它是相对路径
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-01-05
  • 1970-01-01
  • 1970-01-01
  • 2014-07-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多