激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

服務(wù)器之家:專(zhuān)注于服務(wù)器技術(shù)及軟件下載分享
分類(lèi)導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語(yǔ)言|JavaScript|易語(yǔ)言|vb.net|

服務(wù)器之家 - 編程語(yǔ)言 - ASP.NET教程 - asp.net(c#)文件下載實(shí)現(xiàn)代碼

asp.net(c#)文件下載實(shí)現(xiàn)代碼

2019-06-23 10:31ASP.NET之家 ASP.NET教程

本文通過(guò)一個(gè)實(shí)例向大家介紹用C#進(jìn)行Internet通訊編程的一些基本知識(shí)。我們知道.Net類(lèi)包含了請(qǐng)求/響應(yīng)層、應(yīng)用協(xié)議層、傳輸層等層次。

代碼如下:


using System; 
using System.Data; 
using System.Configuration; 
using System.Web; 
using System.Web.Security; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using System.Web.UI.WebControls.WebParts; 
using System.Web.UI.HtmlControls; 
using System.IO; 
public partial class _Default : System.Web.UI.Page 

protected void Page_Load(object sender, EventArgs e) { } 
//TransmitFile實(shí)現(xiàn)下載 
protected void Button1_Click(object sender, EventArgs e) 

/* 微軟為Response對(duì)象提供了一個(gè)新的方法TransmitFile來(lái)解決使用Response.BinaryWrite 下載超過(guò)400mb的文件時(shí)導(dǎo)致Aspnet_wp.exe進(jìn)程回收而無(wú)法成功下載的問(wèn)題。 代碼如下: */ 
Response.ContentType = "application/x-zip-compressed"; Response.AddHeader("Content-Disposition", "attachment;filename=z.zip"); 
string filename = Server.MapPath("DownLoad/z.zip"); Response.TransmitFile(filename); 

//WriteFile實(shí)現(xiàn)下載 
protected void Button2_Click(object sender, EventArgs e) 

/* using System.IO; */ 
string fileName = "asd.txt";//客戶(hù)端保存的文件名 
string filePath = Server.MapPath("DownLoad/aaa.txt");//路徑 
FileInfo fileInfo = new FileInfo(filePath); 
Response.Clear(); 
Response.ClearContent(); 
Response.ClearHeaders(); 
Response.AddHeader("Content-Disposition", "attachment;filename=" + fileName); Response.AddHeader("Content-Length", fileInfo.Length.ToString()); 
Response.AddHeader("Content-Transfer-Encoding", "binary"); 
Response.ContentType = "application/octet-stream"; 
Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312"); 
Response.WriteFile(fileInfo.FullName); 
Response.Flush(); 
Response.End(); 

//WriteFile分塊下載 
protected void Button3_Click(object sender, EventArgs e) 

string fileName = "aaa.txt";//客戶(hù)端保存的文件名 
string filePath = Server.MapPath("DownLoad/aaa.txt");//路徑 
System.IO.FileInfo fileInfo = new System.IO.FileInfo(filePath); 
if (fileInfo.Exists == true) 

const long ChunkSize = 102400;//100K 每次讀取文件,只讀取100K,這樣可以緩解服務(wù)器的壓力 
byte[] buffer = new byte[ChunkSize]; 
Response.Clear(); 
System.IO.FileStream iStream = System.IO.File.OpenRead(filePath); 
long dataLengthToRead = iStream.Length;//獲取下載的文件總大小 
Response.ContentType = "application/octet-stream"; 
Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileName)); while (dataLengthToRead > 0 && Response.IsClientConnected) 

int lengthRead = iStream.Read(buffer, 0, Convert.ToInt32(ChunkSize));//讀取的大小 Response.OutputStream.Write(buffer, 0, lengthRead); 
Response.Flush(); 
dataLengthToRead = dataLengthToRead - lengthRead; 

Response.Close(); 


//流方式下載 
protected void Button4_Click(object sender, EventArgs e) 

string fileName = "aaa.txt";//客戶(hù)端保存的文件名 
string filePath = Server.MapPath("DownLoad/aaa.txt");//路徑 //以字符流的形式下載文件 
FileStream fs = new FileStream(filePath, FileMode.Open); 
byte[] bytes = new byte[(int)fs.Length]; fs.Read(bytes, 0, bytes.Length); 
fs.Close(); 
Response.ContentType = "application/octet-stream"; //通知瀏覽器下載文件而不是打開(kāi) Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8)); 
Response.BinaryWrite(bytes); 
Response.Flush(); 
Response.End(); 


/* 
這里提供4種常用下載方式 以供參考導(dǎo)讀:    
本文通過(guò)一個(gè)實(shí)例向大家介紹用C#進(jìn)行Internet通訊編程的一些基本知識(shí)。我們知道.Net類(lèi)包含了請(qǐng)求/響應(yīng)層、應(yīng)用協(xié)議層、傳輸層等層次。在本程序中,我們運(yùn)用了位于請(qǐng)求/響應(yīng)層的WebRequest類(lèi)以及WebClient類(lèi)等來(lái)實(shí)現(xiàn)高抽象程度的Internet通訊服務(wù)。本程序的功能是完成網(wǎng)絡(luò)文件的下載。    
實(shí)現(xiàn)原理 
程序?qū)崿F(xiàn)的原理比較簡(jiǎn)單,主要用到了WebClient類(lèi)和FileStream類(lèi)。其中WebClient類(lèi)處于System.Net名字空間中,該類(lèi)的主要功能是提供向URI標(biāo)識(shí)的資源發(fā)送數(shù)據(jù)和從URI標(biāo)識(shí)的資源接收數(shù)據(jù)的公共方法。我們利用其中的DownloadFile()方法將網(wǎng)絡(luò)文件下載到本地。然后用FileStream類(lèi)的實(shí)例對(duì)象以數(shù)據(jù)流的方式將文件數(shù)據(jù)寫(xiě)入本地文件。這樣就完成了網(wǎng)絡(luò)文件的下載。    
實(shí)現(xiàn)步驟    
首先,打開(kāi)Visual Studio.Net,新建一個(gè)Visual C#Windows應(yīng)用程序的工程,不妨命名為“MyGetCar”。接著,布置主界面。我們先往主窗體上添加如下控件:兩個(gè)標(biāo)簽控件、兩個(gè)文本框控件、一個(gè)按鈕控件以及一個(gè)狀態(tài)欄控件。 
設(shè)置各控件屬性如下:    
控件類(lèi)型 控件名稱(chēng) 屬性類(lèi)型 屬性值 主窗體 Form1 Text屬性 文件下載器 標(biāo)簽控件 Label1 Text屬性 文件地址: TextAlign屬性 MiddleRight Label2 Text屬性 另存到: TextAlign屬性 MiddleRight 文本框控件 srcAddress Text屬性 (空) tarAddress Text屬性 (空) 按鈕控件 Start FlatStyle屬性 Flat Text屬性 開(kāi)始下載 狀態(tài)欄控件 StatusBar Text屬性 (空)    
其他屬性可為默認(rèn)值,最終的主窗體如下圖所示:          
完成主窗體的設(shè)計(jì),我們接著完成代碼的編寫(xiě)。    
在理解了基本原理的基礎(chǔ)上去完成代碼的編寫(xiě)是相當(dāng)容易。程序中我們主要用到的是WebClient類(lèi),不過(guò)在我們調(diào)用WebClient類(lèi)的實(shí)例對(duì)象前,我們需要用WebRequest類(lèi)的對(duì)象發(fā)出對(duì)統(tǒng)一資源標(biāo)識(shí)符(URI)的請(qǐng)求。 

復(fù)制代碼代碼如下:


try { WebRequest myre=WebRequest.Create(URLAddress); } 
catch(WebException exp){ 
MessageBox.Show(exp.Message,"Error"); 


這是一個(gè)try-catch語(yǔ)句,try塊完成向URI的請(qǐng)求,catch塊則捕捉可能的異常并顯示異常信息。其中的URLAddress為被請(qǐng)求的網(wǎng)絡(luò)主機(jī)名。   在請(qǐng)求成功后,我們就可以運(yùn)用WebClient類(lèi)的實(shí)例對(duì)象中的DownloadFile()方法實(shí)現(xiàn)文件的下載了。其函數(shù)原型如下:   public void DownloadFile( string address, string fileName);   其中,參數(shù)address為從中下載數(shù)據(jù)的 URI,fileName為要接收數(shù)據(jù)的本地文件的名稱(chēng)。   之后我們用OpenRead()方法來(lái)打開(kāi)一個(gè)可讀的流,該流完成從具有指定URI的資源下載數(shù)據(jù)的功能。其函數(shù)原型如下:   public Stream OpenRead(string address);   其中,參數(shù)address同上。   最后就是新建一個(gè)StreamReader對(duì)象從中讀取文件的數(shù)據(jù),并運(yùn)用一個(gè)while循環(huán)體不斷讀取數(shù)據(jù),只到讀完所有的數(shù)據(jù)。    
還有在使用以上方法時(shí),你將可能需要處理以下幾種異常:    
● WebException:下載數(shù)據(jù)時(shí)發(fā)生錯(cuò)誤。    
● UriFormatException:通過(guò)組合 BaseAddress、address 和 QueryString 所構(gòu)成的 URI 無(wú)效。    
這部分的代碼如下:(client為WebClient對(duì)象,在本類(lèi)的開(kāi)頭處聲明即可) 

復(fù)制代碼代碼如下:


statusBar.Text = "開(kāi)始下載文件..."; 
client.DownloadFile(URLAddress,fileName); 
Stream str = client.OpenRead(URLAddress); 
StreamReader reader = new StreamReader(str); 
byte[] mbyte = new byte[100000]; 
int allmybyte = (int)mbyte.Length; 
int startmbyte = 0; 
statusBar.Text = "正在接收數(shù)據(jù)..."; 
while(allmybyte>0){ 
int m = str.Read(mbyte,startmbyte,allmybyte); 
if(m==0) 
break; 
startmbyte+=m; 
allmybyte-=m; 


完成了文件數(shù)據(jù)的讀取工作后,我們運(yùn)用FileStream類(lèi)的實(shí)例對(duì)象將這些數(shù)據(jù)寫(xiě)入本地文件中: 
FileStream fstr = new FileStream(Path,FileMode.OpenOrCreate,FileAccess.Write); fstr.Write(mbyte,0,startmbyte); 
*/

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 国产精品www | 亚洲四播房 | 成人毛片免费网站 | 久久99国产精品免费网站 | 亚洲精品久久久久久 | 性猛aa久久久 | 在线看免电影网站 | 91网站永久免费看 | 久久撸视频| 欧美一级黑人 | 末成年女av片一区二区 | 成人一级黄色 | 九九热国产在线 | 精品国产一区二区三 | 毛片中文字幕 | 欧美激情精品久久久久久黑人 | 国产精品久久久久久久成人午夜 | 日本精品免费观看 | 黄色av网 | 日本精品视频一区二区三区四区 | 91情侣在线偷精品国产 | 久久网综合 | 欧美女孩videos | 老司机一级毛片 | 二区三区四区视频 | 久久久一区二区精品 | 欧美一级高清免费 | 欧美性生活久久 | 国产一级毛片高清视频 | 日本黄色网战 | 91精品观看91久久久久久国产 | 精品一区二区三区毛片 | 一级黄色毛片播放 | 久久成人免费观看 | 欧美老逼| 性欧美在线视频 | 成人一级免费视频 | 国产艳妇av视国产精选av一区 | 亚洲av一级毛片特黄大片 | 49vv看片免费 | 久久久裸体视频 |