小技巧——从网站上下载指定的文件
#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
/// <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