其实需求是这样,
要做一键导出,
有图片,有照片,youhtml,存在不同的文件夹,每次下载都必须下载最新数据,因为FTP是随时更新的。
1.这要是一直下载下载,浏览器一直跳窗口,蛋疼的我都看不下去。所以,只能选择别的办法
2.要是放在WEB下载,我觉得肯定会崩溃的呀,所以想选择客户端直接下载,但是必须得装插件,类型ActionX之类的。头给否认了。我凑,只能想别的办法
3.只能先用WEB把流读下来,压缩,然后传给用户,很蛋疼,这要是谁点了,多考验服务器内存CPU,但是头说了,这样也比插件好,好吧,开始做吧
学习连接
http://www.csharpwin.com/dotnetspace/7579r311.shtml
下面是代码
#region Zip multi files public static int Zip(string destFolder, string[] srcFiles, string folderName, CompressLevel level) { OperFileFTP operFileFTP = new OperFileFTP(); ZipOutputStream zipStream = null; int count = 0; string snappath = "ftp://ip"; try { //Use Crc32 Crc32 crc32 = new Crc32(); Stream ftpoutStream = null; //Create Zip File zipStream = new ZipOutputStream(File.Create(destFolder)); //Specify Level zipStream.SetLevel(Convert.ToInt32(level)); #region Foreach File; foreach (string file in srcFiles) { Uri u; if (file != null) { string filepath = file; string ftpfilepath = file; if (Path.GetExtension(file) == ".jpg") { u = new Uri(snappath); filepath = filepath.Replace("\\", "/"); ftpfilepath = ftpfilepath.Replace("\\", "/"); } else { u = new Uri(ftppath); } filepath = filepath.Substring(0, filepath.LastIndexOf("/")); clsFTP cs = new clsFTP(u, entity.User_Name, entity.Pwd); byte[] buffer = cs.DownloadFile(ftpfilepath); //Specify ZipEntry crc32.Reset(); crc32.Update(buffer); ZipEntry zipEntry = new ZipEntry(Path.Combine(filepath, Path.GetFileName(file))); zipEntry.DateTime = DateTime.Now; zipEntry.Size = buffer.Length; zipEntry.Crc = crc32.Value; //Put file info into zip stream zipStream.PutNextEntry(zipEntry); zipStream.Write(buffer, 0, buffer.Length); count++; } } if (!string.IsNullOrEmpty(folderName)) { byte[] buffer = System.Text.Encoding.Default.GetBytes(folderName); //Specify ZipEntry crc32.Reset(); crc32.Update(buffer); ZipEntry zipEntry = new ZipEntry(Path.Combine("", Path.GetFileName("OperationReport.htm"))); zipEntry.DateTime = DateTime.Now; zipEntry.Size = buffer.Length; zipEntry.Crc = crc32.Value; //Put file info into zip stream zipStream.PutNextEntry(zipEntry); //Put file data into zip stream zipStream.Write(buffer, 0, buffer.Length); count++; } #endregion; if (ftpoutStream != null) ftpoutStream.Flush(); } catch { throw; } finally { //Clear Resource if (zipStream != null) { zipStream.Finish(); zipStream.Close(); } } FileStream stream = new FileStream(destFolder, FileMode.Open); //long cl = stream.ContentLength; HttpContext.Current.Response.Clear(); HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpContext.Current.Server.UrlEncode(Path.GetFileName(destFolder))); int bufferSize = 2048; int readCount; byte[] bufferr = new byte[bufferSize]; char[] cChar = Encoding.ASCII.GetChars(bufferr); //接收FTP文件流 while ((readCount = stream.Read(bufferr, 0, 2048)) > 0) { HttpContext.Current.Response.OutputStream.Write(bufferr, 0, readCount); HttpContext.Current.Response.Flush(); } stream.Flush(); stream.Close(); if (File.Exists(destFolder)) File.Delete(destFolder); return count; } #endregion
方法如上。。
一下为引用代码。。
时间有限,有机会补充
string[] files = new string[3]; Zip(context.Server.MapPath("Tech_" + id + ".zip"), files, str, CompressLevel.Level8);