溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

利用C#怎么將圖片下載到服務(wù)器

發(fā)布時(shí)間:2020-12-23 15:45:52 來(lái)源:億速云 閱讀:199 作者:Leah 欄目:開發(fā)技術(shù)

利用C#怎么將圖片下載到服務(wù)器?相信很多沒(méi)有經(jīng)驗(yàn)的人對(duì)此束手無(wú)策,為此本文總結(jié)了問(wèn)題出現(xiàn)的原因和解決方法,通過(guò)這篇文章希望你能解決這個(gè)問(wèn)題。

C#實(shí)現(xiàn)把圖片下載到服務(wù)器代碼

ASPX頁(yè)面代碼:

<%@ 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>根據(jù)網(wǎng)址把圖片下載到服務(wù)器</title>
</head>
<body>
 <form id="form1" runat="server">
 <div>
 圖片網(wǎng)址:<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頁(yè)面代碼:

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

    //根據(jù)網(wǎng)址下載文件
    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("https://cache.yisu.com/upload/information/20201208/265/32723.gif",@"c:\baidu.gif"); 
DownloadFile方法里的address就是你要拼成的遠(yuǎn)程服務(wù)器上的URL.

看完上述內(nèi)容,你們掌握利用C#怎么將圖片下載到服務(wù)器的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI