C#实现把图片下载到服务器代码

发布时间:2022-04-16 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了C#实现把图片下载到服务器代码脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

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.

好了,小伙伴们是否有了新的认识了呢,希望大家能够喜欢。

脚本宝典总结

以上是脚本宝典为你收集整理的C#实现把图片下载到服务器代码全部内容,希望文章能够帮你解决C#实现把图片下载到服务器代码所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签:C#