asp.net中文件下載功能的實(shí)例代碼
//TransmitFile實(shí)現(xiàn)下載
protected void Button1_Click(object sender, EventArgs e)
{
Response.ContentType = "application/x-zip-compressed";
Response.AddHeader("Content-Disposition", "attachment;filename=z.zip");
string filename = Server.MapPath("DownLoad/aaa.zip");
Response.TransmitFile(filename);
}
//WriteFile實(shí)現(xiàn)下載
protected void Button2_Click(object sender, EventArgs e)
{
string fileName ="aaa.zip";//客戶端保存的文件名
string filePath=Server.MapPath("DownLoad/aaa.zip");//路徑
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.zip";//客戶端保存的文件名
string filePath = Server.MapPath("DownLoad/aaa.zip");//路徑
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.zip";//客戶端保存的文件名
string filePath = Server.MapPath("DownLoad/aaa.zip");//路徑
//以字符流的形式下載文件
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";
//通知瀏覽器下載文件而不是打開
Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8));
Response.BinaryWrite(bytes);
Response.Flush();
Response.End();
}
- Asp.net實(shí)現(xiàn)MVC處理文件的上傳下載功能實(shí)例教程
- ASP.NET Core實(shí)現(xiàn)文件上傳和下載
- ASP.NET?Core實(shí)現(xiàn)動(dòng)態(tài)獲取文件并下載
- asp.net實(shí)現(xiàn)多個(gè)文件同時(shí)下載功能
- ASP.NET實(shí)現(xiàn)從服務(wù)器下載文件問題處理
- asp.net實(shí)現(xiàn)服務(wù)器文件下載到本地的方法
- ASP.Net下載大文件的實(shí)現(xiàn)方法
- ASP.NET 在下載文件時(shí)對(duì)其重命名的思路及實(shí)現(xiàn)方法
- 在ASP.NET中下載文件的實(shí)現(xiàn)代碼
- asp.net 文件下載實(shí)現(xiàn)代碼
- ASP.NET MVC實(shí)現(xiàn)文件下載
相關(guān)文章
ASP.NET WebForm中<%=%>與<%#%>的區(qū)別
這篇文章主要介紹了ASP.NET WebForm中<%=%>與<%#%>的區(qū)別,需要的朋友可以參考下2015-01-01Microsoft Visual Studio 2017 for Mac Preview安裝使用案例分享
這篇文章主要為大家分享了Microsoft Visual Studio 2017 for Mac Preview安裝使用案例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-11-11asp.net音頻轉(zhuǎn)換之.amr轉(zhuǎn).mp3(利用ffmpeg轉(zhuǎn)換法)
AMR轉(zhuǎn)MP3可實(shí)現(xiàn)將手機(jī)上的AMR錄音轉(zhuǎn)換成流行的MP3格式,以適用更廣泛的應(yīng)用。AMR的體積非常小,適用于存儲(chǔ)在手機(jī)中,當(dāng)我們想將在手機(jī)上的音頻上傳到網(wǎng)絡(luò),就需要將其轉(zhuǎn)換成MP3等流行的格式,本文就是介紹asp.net利用ffmpeg轉(zhuǎn)換法將.amr轉(zhuǎn).mp3的方法,下面來一起看看吧。2016-12-12