用程序实现文件的下载方法很多,在.net中有个webclient类可轻松实现文件的下载,但在framework精简版中却没有提供这个类,但可以利用webservice来解决.代码如下:
webservice:

 /// <summary>
 /// 下载文件
 /// </summary>
 /// <param name="FileName"></param>
 /// <returns></returns>
 [WebMethod]
 public byte[] downloadFile(string FileName)
 {
     FileStream fs = new FileStream(Server.MapPath(FileName), FileMode.Open, FileAccess.Read);
     byte[] fileData = new Byte[fs.Length];
     fs.Read(fileData, 0, (int)fs.Length);
     fs.Close();
     return fileData;
 }

客户端代码:

  localhost.PDAService ws=new WindowsApplication2.localhost.PDAService();
                                           //localhost.PDAService是webservice的代理类
  byte[] b=ws.downloadFile("VisitSys.exe");//要下载的文件
  FileStream fs=new FileStream("VisitSys.exe",FileMode.OpenOrCreate,FileAccess.Write);
  fs.Write(b,0,b.Length);
  fs.Close();
  MessageBox.Show("OK");
以上方法不适应大文件的下载,因为这是一次性把文件读入内存的.

MapPath方法的试验

状态:
1。IIS的根目录==>c:\inetpub\wwwroot
2。虚拟目录"oas"==>F:\hskama\oas
3。在http://localhost/oas/sys/others/mkm.aspx执行Response.Write(XXX)

结果:
Server.MapPath(".")              F:\hskama\oas\sys\others
Server.MapPath("~")             F:\hskama\oas
Server.MapPath("")               F:\hskama\oas\sys\others
Server.MapPath("..")             F:\hskama\oas\sys
Server.MapPath("~/..")          c:\inetpub\wwwroot

相关文章:

  • 2022-02-14
  • 2021-07-26
  • 2022-12-23
  • 2021-06-29
  • 2022-12-23
  • 2021-11-13
  • 2022-12-23
  • 2021-07-18
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-10-14
  • 2022-02-04
  • 2021-07-31
  • 2022-02-23
相关资源
相似解决方案