软讯网络 > 编程语言 > .NET > C#.NET > 利用XMLHTTP下载文件
【标 题】:利用XMLHTTP下载文件
【关键字】:
XMLHTTP
【来 源】:http://blog.csdn.net/Jon_Pilot/archive/2006/09/13/1217387.aspx
利用XMLHTTP下载文件

添加引用 COM Microsoft Xml 3.0
protected void Page_Load(object sender, EventArgs e)
{
string url = "http://i.microsoft.com/h/all/i/ms_masthead_8x6a_ltr.jpg";
string fileName = url.Substring(url.LastIndexOf("/") + 1);
string filePath = Request.PhysicalApplicationPath;
if(filePath.EndsWith("\"))
{
filePath += "\";
}
XMLHTTP xmlhttp = new XMLHTTPClass();
xmlhttp.open("get", url, false, null, null);
xmlhttp.send("");
if (xmlhttp.readyState == 4)
{
if (File.Exists(filePath + fileName))
{
File.Delete(filePath + fileName);
}
FileStream fs = new FileStream(filePath + fileName, FileMode.CreateNew);
BinaryWriter bw = new BinaryWriter(fs);
bw.Write((byte[])xmlhttp.responseBody);
bw.Close();
fs.Close();
Response.Write("文件已经得到!");
Response.Write("<br><a href=" + Request.ApplicationPath + "/" + fileName + ">" + "查看" + fileName + "</a>");
}
else
{
Response.Write(xmlhttp.statusText);
}
Response.End();
}