C#实现把图片下载到服务器代码
程序员文章站
2023-12-21 21:17:16
c#实现把图片下载到服务器代码
aspx页面代码:
<%@ page language="c#" autoeventwireup="true" codeb...
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="/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>
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(); string m_keleyipicture = server.mappath("/hovertreeimages/keleyi.jpg"); //根据网址下载文件 m_hvtwebclient.downloadfile(textboximgurl.text, m_keleyipicture); hvtimg.imageurl = "/hovertreeimages/keleyi.jpg"; ltltips.text = string.empty; } catch(exception ex) { ltltips.text = ex.tostring(); } } } }
另外给大家分享一下下载图片的核心方法的思路
using system.net; webclient myclient = new webclient(); myclient.downloadfile("http://www.baidu.com/img/sslm_logo.gif",@"c:\baidu.gif"); downloadfile方法里的address就是你要拼成的远程服务器上的url.
好了,小伙伴们是否有了新的认识了呢,希望大家能够喜欢。