C implementation downloads the picture to the server code

  • 2021-07-09 07:50:22
  • OfStack

The C # implementation downloads the picture to the server code

ASPX page code:


<%@ 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> Download the picture to the server according to the website </title>
</head>
<body>
 <form id="form1" runat="server">
 <div>
  Image website: <br /><asp:TextBox runat="server" ID="textBoxImgUrl" Width="500" Text="/hvtimg/201508/cnvkv745.jpg" />
  <br /> <asp:Button runat="server" ID="btnImg" Text=" Download " OnClick="btnImg_Click" />
  <br /><asp:Image runat="server" ID="hvtImg" />
  <br />
  <asp:Literal runat="server" ID="ltlTips" />
 </div>
 </form>
</body>
</html>

cs page code:


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");

    // Download files based on URL 
    m_hvtWebClient.DownloadFile(textBoxImgUrl.Text, m_keleyiPicture);

    hvtImg.ImageUrl = "/hovertreeimages/keleyi.jpg";
    ltlTips.Text = string.Empty;
   }
   catch(Exception ex)
   {
    ltlTips.Text = ex.ToString();
   }

  }
 }
}

In addition, I would like to share with you the idea of the core method of downloading pictures under 1


using System.Net;
      WebClient myclient = new WebClient();
      myclient.DownloadFile("http://www.baidu.com/img/sslm_logo.gif",@"c:\baidu.gif"); 
DownloadFile In the method address Is the remote server you want to put together URL.

Ok, do you have a new understanding? I hope you can like it.


Related articles: