根据网址把图片下载到服务器C#代码
ASPX页面代码:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="GetPictureByUrl.aspx.cs" Inherits="HoverTreeMobile.GetPictureByUrl" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <meta name="viewport" content="width=device-width, initial-scale=1" /> <title>根据网址把图片下载到服务器 - 何问起</title> </head> <body> <form id="form1" runat="server"> <div> 图片网址:<br /><asp:TextBox runat="server" ID="textBoxImgUrl" Width="500" Text="http://hovertree.com/hvtimg/201508/cnvkv745.jpg" /> <br /> <asp:Button runat="server" ID="btnImg" Text="下载" OnClick="btnImg_Click" /> <br /><asp:Image runat="server" ID="hvtImg" /> <br /> <asp:Literal runat="server" ID="ltlTips" /> </div> </form> </body> </html>
猜谜语:
http://m.hovertree.com/miyu/bjae/4gwevb53.htm
cs页面代码:
using System; namespace HoverTreeMobile { public partial class GetPictureByUrl : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void btnImg_Click(object sender, EventArgs e) { try { System.Net.WebClient m_hvtWebClient = new System.Net.WebClient(); //如果不是指定格式图片 //例如http://hovertree.com/hvtart/bjae/t2lo8pf7.htm 是htm文件,不是图片 if (!(textBoxImgUrl.Text.EndsWith(".jpg") || textBoxImgUrl.Text.EndsWith(".gif") || textBoxImgUrl.Text.EndsWith(".png"))) { ltlTips.Text = "输入的不是指定格式的图片的网址"; return; } //生成随机的图片文件名 string m_picFileName = HoverTree.HoverTreeFrame.Utils.GetHoverTreeString()+ HoverTree.HoverTreeFrame.HoverString.GetLastStr(textBoxImgUrl.Text,4); string m_keleyiPicture = Server.MapPath("/hovertreeimages/"+ m_picFileName); //根据网址下载文件 m_hvtWebClient.DownloadFile(textBoxImgUrl.Text, m_keleyiPicture); hvtImg.ImageUrl = "/hovertreeimages/" + m_picFileName; ltlTips.Text = string.Empty; } catch(Exception ex) { ltlTips.Text = ex.ToString(); } } } }
//生成随机的图片文件名 string m_picFileName = HoverTree.HoverTreeFrame.Utils.GetHoverTreeString()+ HoverTree.HoverTreeFrame.HoverString.GetLastStr(textBoxImgUrl.Text,4);
以上代码,请下载源代码查看详细实现方法。部分可到 http://hovertree.com/hvtart/bjae/tlhx2yn2.htm 查看
HoverTree 开源项目
新增根据网址把图片下载到服务器功能
请看 HoverTreeMobile 项目
http://hovertree.com
何问起
源代码下载 http://hovertree.com/down/
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· 使用C#创建一个MCP客户端
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· Windows编程----内核对象竟然如此简单?