通过c#进行网络文件的检查并进行下载操作
2018-01-20 18:05
561 查看
本人采用webfrom进行web端的开发首先看下页面设计的部分代码:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="TestImg.WebForm1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="ImgUrl" runat="server"></asp:TextBox>
<br />
<asp:Button ID="Button1" runat="server" Text="输入地址准备下载图片"
onclick="Button1_Click" />
<asp:Button ID="CreateNewImg" runat="server" Text="生成圆形图片"
onclick="CreateNewImg_Click" />
</div>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
<br /><asp:Image runat="server" ID="hvtImg" Width="30px" Height="30px" />
<br /><asp:Image runat="server" ID="newImg" Width="70px" Height="70px" />
<asp:TextBox ID="CheckUrl" runat="server"></asp:TextBox>
<br />
<asp:Button ID="BtnCheckUrl" runat="server" Text="检查地址是否可请求"
onclick="BtnCheckUrl_Click" />
</form>
</body>
</html>
在看下后台的代码
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.IO; namespace TestImg { public partial class WebForm1 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } /// <summary> /// 下载图片 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void Button1_Click(object sender, EventArgs e) { t 4000 ry { System.Net.WebClient m_hvtWebClient = new System.Net.WebClient(); //如果不是指定格式图片 string c = ImgUrl.Text.ToString(); //if (!(ImgUrl.Text.EndsWith(".jpg") // || ImgUrl.Text.EndsWith(".gif") // || ImgUrl.Text.EndsWith(".png"))) //{ // Response.Write("<script>非图片格式</script>"); // .Text = "输入的不是指定格式的图片的网址"; // return; //} //生成随机的图片文件名 // string m_picFileName = HoverTree.HoverTreeFrame.Utils.GetHoverTreeString() + HoverTree.HoverTreeFrame.HoverString.GetLastStr(textBoxImgUrl.Text, 4); string m_picFileName = "5.jpg"; string m_keleyiPicture = Server.MapPath("/img/" + m_picFileName); //根据网址下载文件 m_hvtWebClient.DownloadFile(c, m_keleyiPicture); bool checkfile = File.Exists(m_keleyiPicture); if (checkfile) { hvtImg.ImageUrl = "/img/" + m_picFileName; Label1.Text = "下载成功"; } else { hvtImg.ImageUrl = "/img/" + m_picFileName; Label1.Text = "下载失败"; } } catch (Exception) { } } /// <summary> /// 1检查是否存在 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void BtnCheckUrl_Click(object sender, EventArgs e) { string url = this.CheckUrl.Text.ToString(); bool CheckUrls = UrlIsExist(url); if (CheckUrls) { Label1.Text = "文件存在"; } else { Label1.Text = "文件不存在"; } } public static bool UrlIsExist(string URL) { try { System.Net.WebRequest request = System.Net.WebRequest.Create(URL); request.Timeout = 10000; System.Net.WebResponse response = request.GetResponse(); return true; } catch { return false; } } protected void CreateNewImg_Click(object sender, EventArgs e) { } } }
在此代码中,可以进行请求网络的文件请求,进行文件是否存在,如果存在的话,可以进行网络下载到指定的位置。
相关文章推荐
- C#对FTP的操作(上传,下载,重命名文件,删除文件,文件存在检查)
- C#通过一个密码对文件进行加密解密操作
- C#对FTP的操作(上传,下载,重命名文件,删除文件,文件存在检查)
- C#通过Socket进行网络传输文件
- C#对FTP的操作(上传,下载,重命名文件,删除文件,文件存在检查)
- 转载文件操作美文 asp.net(C#)对文件进行操作
- .net,C#,Ftp各种操作,上传,下载,删除文件,创建目录,删除目录,获得文件列表等
- C# 文件操作(上传 下载 删除 文件列表...)
- C# 文件操作(上传 下载 删除 文件列表...)
- C# 的泛型以及网络下载Xml文件和读取Xml文件
- C#中将某一文件拖向应用程序后打开或进行其它操作的实现
- C#中将某一文件拖向应用程序后打开或进行其它操作的实现
- C# 文件操作(上传 下载 删除 文件列表...)
- .net,C#,Ftp各种操作,上传,下载,删除文件,创建目录,删除目录,获得文件列表等 收藏
- C# 文件操作(上传 下载 删除 文件列表...)(转)
- asp.net(c#)从url网络地址下载图片或文件到本地硬盘
- C# 文件操作(上传 下载 删除 文件列表...)
- C# 文件操作(上传 下载 删除 文件列表...)
- [导入].net,C#,Ftp各种操作,上传,下载,删除文件,创建目录,删除目录,获得文件列表等
- C#对文件进行操作