开发者

How to open file with its application(the file not exist in the web server)?

开发者 https://www.devze.com 2023-01-13 23:47 出处:网络
I want to ask how to open a specific file (the file is out of the server, i have a relative path to it stored in config file) with its application, when clicking on a specific link button or hyper lin

I want to ask how to open a specific file (the file is out of the server, i have a relative path to it stored in config file) with its application, when clicking on a specific link button or hyper link...

like :

opening

.docx with word.

or

.pdf with acrobat reader

i tried several methods but , i get different errors like

Cannot use a leading .. to exit above the top directory

my .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"];
            开发者_Python百科    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 "";
        }


In order to get the full file path on the server you'll want to use Server.MapPath.

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

Edit: After that you'll need the Process object to "run" it:

System.Diagnostics.Process.Start(fullFileName);

Edit 2: If you want the file to be opened on the client side, your best bet is to create and HTTP Handler and set the appropriate mime type on your response before streaming it out from your handler.

Edit 3: Code to stream a file out to client.

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;
  }   
}


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

0

精彩评论

暂无评论...
验证码 换一张
取 消