Web浏览器导出FTP服务器上的文件

开发思路:
1.代码登录ftp服务器下载文件到服务器
2.通过web浏览器下载服务器上的文件


using
System; using System.Collections; using System.Configuration; using System.Data; using System.Linq; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Xml.Linq; using System.Net; using System.IO; public partial class Test : System.Web.UI.Page { public string FtpFilePath { get { return Request["filepath"]; } } private string _FtpLoginUserName = "test"; private string _FtpLoginPassword = "123456"; protected void Page_Load(object sender, EventArgs e) { if (string.IsNullOrEmpty(FtpFilePath)) { Response.Write("缺少重要参数:文件路径"); Response.End(); } else { //Download("ftp://192.168.0.130/Attend_Data/2015/KQ_DETAIL_201504.csv"); Download(FtpFilePath); } } #region FTP服务器下载文件 /// <summary> /// FTP服务器下载文件 /// </summary> /// <param name="filePath"></param> /// <param name="fileName"></param> private void Download(string ftpFilePath) { FtpWebRequest reqFTP; try { string filePath = AppDomain.CurrentDomain.BaseDirectory + "temp\AttendanceDataExport\";// + "fileName.csv"; if (!Directory.Exists(filePath)) { Directory.CreateDirectory(filePath); } string ftpFileNmae = ""; ftpFileNmae = ftpFilePath.Substring(ftpFilePath.LastIndexOf('/') + 1); if (string.IsNullOrEmpty(ftpFileNmae)) { ftpFileNmae = "KQ_DETAIL_" + DateTime.Now.Ticks + ".csv"; } string fileName = filePath + ftpFileNmae; if (File.Exists(fileName)) { try { File.Delete(fileName); } catch (Exception ex1) { } } FileStream outputStream = new FileStream(fileName, FileMode.Create); reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpFilePath)); reqFTP.Method = WebRequestMethods.Ftp.DownloadFile; reqFTP.UseBinary = true; reqFTP.Credentials = new NetworkCredential(_FtpLoginUserName, _FtpLoginPassword); FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse(); Stream ftpStream = response.GetResponseStream(); long cl = response.ContentLength; int bufferSize = 2048; int readCount; byte[] buffer = new byte[bufferSize]; readCount = ftpStream.Read(buffer, 0, bufferSize); while (readCount > 0) { outputStream.Write(buffer, 0, readCount); readCount = ftpStream.Read(buffer, 0, bufferSize); } ftpStream.Close(); outputStream.Close(); response.Close(); ResponseExcel(fileName); } catch (System.Threading.ThreadAbortException) { } catch (Exception ex) { Response.Write("Download error" + ex.Message); } } /// <summary> /// 浏览器导出 /// </summary> /// <param name="fileName"></param> private void ResponseExcel(string fileName) { try { System.IO.FileInfo file = new System.IO.FileInfo(fileName); HttpContext.Current.Response.Clear(); HttpContext.Current.Response.Charset = "GB2312"; HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8; // 添加头信息,为"文件下载/另存为"对话框指定默认文件名 if (Page.Request.Browser.Browser == "IE") { HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpContext.Current.Server.UrlEncode(file.Name)); } else { HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name); } // 添加头信息,指定文件大小,让浏览器能够显示下载进度 HttpContext.Current.Response.AddHeader("Content-Length", file.Length.ToString()); // 指定返回的是一个不能被客户端读取的流,必须被下载 HttpContext.Current.Response.ContentType = "application/ms-excel"; // 把文件流发送到客户端 HttpContext.Current.Response.WriteFile(file.FullName); // 停止页面的执行 HttpContext.Current.Response.End(); } catch (System.Threading.ThreadAbortException) { } catch (Exception ex) { this.Response.Write(ex.Source + ex.Message + ex.StackTrace); } } #endregion }
原文地址:https://www.cnblogs.com/stevenjson/p/4530636.html