asp.net從ftp下載大文件再輸出瀏覽器的實現(xiàn)(對文件同時進行讀寫操作)
當前位置:點晴教程→知識管理交流
→『 技術文檔交流 』
很多時候安全起見,不會讓用戶通過瀏覽器直接從ftp下載文件。這時我們需要web從ftp下載文件再輸出到瀏覽器,對于小文件我們可以一次性讀到memorystream,然后輸出;但是大文件這樣實現(xiàn)就會導致服務器內(nèi)存爆炸,此時我們可以利用filestream,一邊從ftp下載數(shù)據(jù)到本地,一邊從本地輸出到用戶客戶端,這樣對于再大的文件都不會影響內(nèi)存。 以下是通過異步的方式實現(xiàn)對下載文件同時進行讀寫操作的代碼(.net framwork 4.5),MD5驗證一致: using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; namespace WebApplication6 { public partial class WebForm1 : System.Web.UI.Page { Aspose.Network.Ftp.FtpClient ftpclient = new Aspose.Network.Ftp.FtpClient("xxxx", "xxx", "xxx"); protected void Page_Load(object sender, EventArgs e) { } string fileName = "C4F4EEP0CX00_ECN-52966A.exe"; string downloadPath = string.Empty; string refilename = string.Empty; public async void DownLoadFileAsync() { string extension = Path.GetExtension(fileName); refilename = Path.GetFileNameWithoutExtension(fileName) + DateTime.Now.ToString("yyyyMMddHHmmssffff") + extension; downloadPath = Server.MapPath(Path.Combine("tempFile", refilename)); try { ftpclient.Connect(); ftpclient.Login(); ftpclient.KeepAlive(); //Task.Run()需要.net4.5及以上版本的支持 await Task.Run(() => { ftpclient.Download("CLIENT_TMP//" + fileName, downloadPath); }); //以下代碼支持.net 4.0 ,但是需要額外在項目中加一個類TaskEx //await Task.Factory.StartNew(() => { ftpclient.Download("CLIENT_TMP//" + fileName, downloadPath); }); } catch { throw; } finally { ftpclient.Disconnect(); } } //要使用異步,頁面配置頭需要加Async="true" //<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication6.WebForm1" Async="true" %> protected void Button1_Click(object sender, EventArgs e) { try { //異步從ftp下載文件到web服務器本地文件夾 DownLoadFileAsync(); Response.Clear();//清空緩沖區(qū) Response.Buffer = false;//不從緩沖區(qū)返回數(shù)據(jù) Response.ContentType = "application/ctet-stream";//設置輸出流 Response.AddHeader("Content-Disposition", "attachment; filename=" + fileName + ";"); Response.AddHeader("Connection", "Keep-Alive");//長連接 Thread.Sleep(2000);//等待異步創(chuàng)建文件成功,避免fileStream初始化失敗 //FileAccess.Read表示當前流只能讀,不能寫,如果后續(xù)有寫操作會報錯 //FileShare.ReadWrite 表示當前流在操作磁盤文件過程中其他流可以讀也可以寫. using (FileStream fileStream = new FileStream(downloadPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) { using (BinaryReader br = new BinaryReader(fileStream)) { int length = 2048; //獲取br.BaseStream.Length時值會隨著ftp下載而變化,每次獲取都會更新 //br.ReadBytes()讀取完后br.BaseStream.Position會前移. while (br.BaseStream.Position < br.BaseStream.Length) { length = br.BaseStream.Length - br.BaseStream.Position > 2048 ? 2048 : (int)(br.BaseStream.Length - br.BaseStream.Position); Response.BinaryWrite(br.ReadBytes(length)); } } } } catch (Exception ex) { Response.Write($"<script>alert(''下載失敗:{ex.Message}'')</script>"); } finally { //下載完成后刪除本地文件夾的文件 try { File.Delete(downloadPath); } catch { } Response.End(); } } } } 該文章在 2021/3/3 11:12:04 編輯過 |
關鍵字查詢
相關文章
正在查詢... |