就他吧-9ta8为您提供:身份证查询、15位转16位身份证,手机号码归属地查询,IP地址查询服务,城市天气预报查询,列车时刻表简易快速查询等等查询服务,就他吧欢迎您的光临!!

小技巧——从网站上下载指定的文件

#region 从网站上下载指定的文件 return string
  /// <summary>
  /// 从网站上下载指定的文件
  /// </summary>
  /// <param name="request">Request</param>
  /// <param name="filePathOfURL">FillPath,如"http://localhost/CSharpWebApp/asd.doc"</param>
  /// <returns>string</returns>
  /// COM-->Microsoft XML,v5.0   using MSXML2;
  public static string FileDownLoadFromWebSite(System.Web.HttpRequest request,string filePathOfURL)
  {
   string rtn = "";
   try
   {
    string Url = "http://dotnet.aspx.cc/Images/banner.gif";
    //Url = "http://localhost/CSharpWebApp/asd.doc";
    Url = filePathOfURL;
    string StringFileName = Url.Substring(Url.LastIndexOf("/") + 1);
    string StringFilePath = request.PhysicalApplicationPath;
    if(!StringFilePath.EndsWith("\\"))
     StringFilePath += "\\";
    MSXML2.XMLHTTP _xmlhttp = new MSXML2.XMLHTTPClass();
    _xmlhttp.open("GET",Url,false,null,null);
    _xmlhttp.send("");
    if( _xmlhttp.readyState == 4 )
    {
     if(System.IO.File.Exists(StringFilePath + StringFileName))
      System.IO.File.Delete(StringFilePath + StringFileName);
     System.IO.FileStream fs = new System.IO.FileStream(StringFilePath + StringFileName, System.IO.FileMode.CreateNew);
     System.IO.BinaryWriter w = new System.IO.BinaryWriter(fs);
     w.Write((byte[])_xmlhttp.responseBody);
     w.Close();
     fs.Close();
     rtn = "文件已经得到。<br><a href='" + request.ApplicationPath + "/" + StringFileName +"' target='_blank'>";
     //Response.Write ("文件已经得到。<br><a href='" + Request.ApplicationPath + "/" + StringFileName +"' target='_blank'>");
     rtn = rtn + "查看" + StringFileName + "</a>";
    }
    else
     rtn =_xmlhttp.statusText;
   }
   catch{}
   return rtn;
  }
  #endregion 从网站上下载指定的文件 return string
posted on 2005-07-26 17:51  振河  阅读(1422)  评论(4编辑  收藏  举报

  就他吧-9ta8伴您开心每一天