C#下载远程服务器文件到本地服务器
<%@ Page Language="C#" ContentType="text/html" ResponseEncoding="gb2312" %>
<script language="c#" runat="server">
private void Page_Load(object sender,EventArgs e)
{
}
protected void ImgUrl_Click(object sender,EventArgs e)
{
string strURL = ImgUrl.Text.ToString().Trim();
string strName = ImgName.Text.ToString().Trim();
if (strURL == "")
{
Msg.Text = "<font color=red>请输入图片地址!</font>";
return;
}
if (strName == "")
strName = "download.gif";
System.Net.WebClient myWebClient = new System.Net.WebClient();
Msg.Text = "<font color=green>图片下载成功!</font><br><br><img src=\"" + strName + "\">";
try
{
myWebClient.DownloadFile(strURL,Server.MapPath(strName));
}
catch
{
Msg.Text = "<font color=red>出现错误,可能输入地址有误!</font>";
return;
}
}
</script>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>下载图片</title>
<style type="text/css">
td {
font-size:12px;
}
</style>
</head>
<body>
<form runat="server">
<table width="500" height="143" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td>请输入图片路径:
<asp:TextBox ID="ImgUrl" runat="server" Width="300" Text="http://"; onMouseOver="this.select()"></asp:TextBox>
<br><br>
请输入保存名称:
<asp:TextBox ID="ImgName" runat="server" Width="200"></asp:TextBox>
<asp:Button ID="Get" runat="server" Text="下载" onClick="ImgUrl_Click"></asp:Button>
<br><br>
<asp:Label ID="Msg" runat="server"></asp:Label>
</td>
</tr>
</table>
</form>
</body>
</html>
<script language="c#" runat="server">
private void Page_Load(object sender,EventArgs e)
{
}
protected void ImgUrl_Click(object sender,EventArgs e)
{
string strURL = ImgUrl.Text.ToString().Trim();
string strName = ImgName.Text.ToString().Trim();
if (strURL == "")
{
Msg.Text = "<font color=red>请输入图片地址!</font>";
return;
}
if (strName == "")
strName = "download.gif";
System.Net.WebClient myWebClient = new System.Net.WebClient();
Msg.Text = "<font color=green>图片下载成功!</font><br><br><img src=\"" + strName + "\">";
try
{
myWebClient.DownloadFile(strURL,Server.MapPath(strName));
}
catch
{
Msg.Text = "<font color=red>出现错误,可能输入地址有误!</font>";
return;
}
}
</script>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>下载图片</title>
<style type="text/css">
td {
font-size:12px;
}
</style>
</head>
<body>
<form runat="server">
<table width="500" height="143" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td>请输入图片路径:
<asp:TextBox ID="ImgUrl" runat="server" Width="300" Text="http://"; onMouseOver="this.select()"></asp:TextBox>
<br><br>
请输入保存名称:
<asp:TextBox ID="ImgName" runat="server" Width="200"></asp:TextBox>
<asp:Button ID="Get" runat="server" Text="下载" onClick="ImgUrl_Click"></asp:Button>
<br><br>
<asp:Label ID="Msg" runat="server"></asp:Label>
</td>
</tr>
</table>
</form>
</body>
</html>