C#實現(xiàn)把圖片下載到服務器代碼
更新時間:2015年11月16日 08:58:55 投稿:hebedich
本文給大家分享的是實現(xiàn)這樣一個功能,想將遠程服務器的圖片下載到本地主機,圖片的名稱就是數(shù)據庫的一個字段,圖片不是以二進制的形式存儲在數(shù)據庫的,數(shù)據庫存儲的只是圖片的名詞。
C#實現(xiàn)把圖片下載到服務器代碼
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.
好了,小伙伴們是否有了新的認識了呢,希望大家能夠喜歡。
相關文章
在jquery repeater中添加設置日期,下拉,復選框等控件
JQueryElement 更新到了 3.5.1, 今天給大家主要講下如何在 Repeater 的模板中添加設置一些控件.2011-10-10ASP.NET MVC3 SEO優(yōu)化:利用Routing特性提高站點權重
這篇文章主要介紹了ASP.NET MVC3 SEO優(yōu)化:利用Routing特性消除多個路徑指向同一個Action,從而提高站點權重,需要的朋友可以參考下。2016-06-06aspnet_regsql.exe 工具注冊數(shù)據庫的圖文方法
自 ASP.NET 2.0 起,微軟在 ASP.NET 上新增了很多功能,其中包括 Membership , Role , Profile 等等諸多功能2010-03-03