C#实现把图片下载到服务器代码
C#实现把图片下载到服务器代码
ASPX页面代码:
<%@PageLanguage="C#"AutoEventWireup="true"CodeBehind="GetPictureByUrl.aspx.cs"Inherits="HoverTreeMobile.GetPictureByUrl"%> <!DOCTYPEhtml> <htmlxmlns="http://www.w3.org/1999/xhtml"> <headrunat="server"> <metahttp-equiv="Content-Type"content="text/html;charset=utf-8"/> <metaname="viewport"content="width=device-width,initial-scale=1"/> <title>根据网址把图片下载到服务器</title> </head> <body> <formid="form1"runat="server"> <div> 图片网址:<br/><asp:TextBoxrunat="server"ID="textBoxImgUrl"Width="500"Text="/hvtimg/201508/cnvkv745.jpg"/> <br/><asp:Buttonrunat="server"ID="btnImg"Text="下载"OnClick="btnImg_Click"/> <br/><asp:Imagerunat="server"ID="hvtImg"/> <br/> <asp:Literalrunat="server"ID="ltlTips"/> </div> </form> </body> </html>
cs页面代码:
usingSystem; namespaceHoverTreeMobile { publicpartialclassGetPictureByUrl:System.Web.UI.Page { protectedvoidPage_Load(objectsender,EventArgse) { } protectedvoidbtnImg_Click(objectsender,EventArgse) { try { System.Net.WebClientm_hvtWebClient=newSystem.Net.WebClient(); stringm_keleyiPicture=Server.MapPath("/hovertreeimages/keleyi.jpg"); //根据网址下载文件 m_hvtWebClient.DownloadFile(textBoxImgUrl.Text,m_keleyiPicture); hvtImg.ImageUrl="/hovertreeimages/keleyi.jpg"; ltlTips.Text=string.Empty; } catch(Exceptionex) { ltlTips.Text=ex.ToString(); } } } }
另外给大家分享一下下载图片的核心方法的思路
usingSystem.Net; WebClientmyclient=newWebClient(); myclient.DownloadFile("http://www.baidu.com/img/sslm_logo.gif",@"c:\baidu.gif"); DownloadFile方法里的address就是你要拼成的远程服务器上的URL.
好了,小伙伴们是否有了新的认识了呢,希望大家能够喜欢。