利用XMLHTTP下载文件的步骤
添加引用 COM Microsoft Xml 3.0
protectedvoidPage_Load(objectsender,EventArgse)
{
stringurl="http://i.microsoft.com/h/all/i/ms_masthead_8x6a_ltr.jpg";
stringfileName=url.Substring(url.LastIndexOf("/")+1);
stringfilePath=Request.PhysicalApplicationPath;
if(filePath.EndsWith("/"))
{
filePath+="/";
}
XMLHTTPxmlhttp=newXMLHTTPClass();
xmlhttp.open("get",url,false,null,null);
xmlhttp.send("");
if(xmlhttp.readyState==4)
{
if(File.Exists(filePath+fileName))
{
File.Delete(filePath+fileName);
}
FileStreamfs=newFileStream(filePath+fileName,FileMode.CreateNew);
BinaryWriterbw=newBinaryWriter(fs);
bw.Write((byte[])xmlhttp.responseBody);
bw.Close();
fs.Close();
Response.Write("文件已经得到!");
Response.Write("<br><ahref="+Request.ApplicationPath+"/"+fileName+">"+"查看"+fileName+"</a>");
}
else
{
Response.Write(xmlhttp.statusText);
}
Response.End();
}
{
stringurl="http://i.microsoft.com/h/all/i/ms_masthead_8x6a_ltr.jpg";
stringfileName=url.Substring(url.LastIndexOf("/")+1);
stringfilePath=Request.PhysicalApplicationPath;
if(filePath.EndsWith("/"))
{
filePath+="/";
}
XMLHTTPxmlhttp=newXMLHTTPClass();
xmlhttp.open("get",url,false,null,null);
xmlhttp.send("");
if(xmlhttp.readyState==4)
{
if(File.Exists(filePath+fileName))
{
File.Delete(filePath+fileName);
}
FileStreamfs=newFileStream(filePath+fileName,FileMode.CreateNew);
BinaryWriterbw=newBinaryWriter(fs);
bw.Write((byte[])xmlhttp.responseBody);
bw.Close();
fs.Close();
Response.Write("文件已经得到!");
Response.Write("<br><ahref="+Request.ApplicationPath+"/"+fileName+">"+"查看"+fileName+"</a>");
}
else
{
Response.Write(xmlhttp.statusText);
}
Response.End();
}
本文地址:http://www.45fan.com/dnjc/70288.html