<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm3.aspx.cs" Inherits="布局.WebForm3" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID ="btnExport" runat ="server" Text ="导出"
onclick="btnExport_Click" />
</div>
</form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO; namespace 布局
{
public partial class WebForm3 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{ } protected void btnExport_Click(object sender, EventArgs e)
{
try
{
List<FileInfo> fileList = new List<FileInfo>();
string fileName1 = @"D:\Export\新建 Microsoft Office Excel 工作表1.xlsx";
string fileName2 = @"D:\Export\新建 Microsoft Office Excel 工作表2.xlsx";
fileList.Add(new FileInfo(fileName1));
fileList.Add(new FileInfo(fileName2)); string fileName = "Export_" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".zip";
//调用方法
string targetZipFilePath =Server.MapPath("/")+ "Export\\" + fileName;// 扩展名可随意
//压缩csv文件
FileCompression.Compress(fileList, targetZipFilePath, , ); //把压缩文件输出到浏览器
System.Web.HttpResponse Response = System.Web.HttpContext.Current.Response;
Response.Clear();
Response.ContentType = "application/zip";
Response.AddHeader("Content-Disposition", "attachment; FileName=" + HttpUtility.UrlEncode(fileName));
Response.BinaryWrite(File2Bytes(targetZipFilePath));
Response.OutputStream.Flush();
Response.OutputStream.Close();
Response.End();//这句代码不能少,否则可能导致输出的文件数据不全,导致解压时出现压缩格式未知的错误 //删除产生的压缩文件
try
{
FileInfo fi1 = new FileInfo(targetZipFilePath);
fi1.Delete();
}
catch (Exception ex)
{ }
}
catch (Exception ex)
{
ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "CreateExcelErrorTip", "alert('Export Failed!');", true);
return;
}
} public static byte[] File2Bytes(string path)
{
if (!System.IO.File.Exists(path))
{
return new byte[];
} FileInfo fi = new FileInfo(path);
byte[] buff = new byte[fi.Length]; FileStream fs = fi.OpenRead();
fs.Read(buff, , Convert.ToInt32(fs.Length));
fs.Close(); return buff;
}
}
}

压缩用到的

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using ICSharpCode.SharpZipLib.Zip;
using System.Threading; namespace 布局
{
public class FileCompression
{
public FileCompression()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
#region 加密、压缩文件 /// <summary>
/// 压缩文件
/// </summary>
/// <param name="fileNames">要打包的文件列表</param>
/// <param name="GzipFileName">目标文件名</param>
/// <param name="CompressionLevel">压缩品质级别(0~9)</param>
/// <param name="SleepTimer">休眠时间(单位毫秒)</param>
public static void Compress(List<FileInfo> fileNames, string GzipFileName, int CompressionLevel, int SleepTimer)
{
ZipOutputStream s = new ZipOutputStream(File.Create(GzipFileName));
try
{
s.SetLevel(CompressionLevel); //0 - store only to 9 - means best compression
foreach (FileInfo file in fileNames)
{
FileStream fs = null;
try
{
fs = file.Open(FileMode.Open, FileAccess.ReadWrite);
}
catch
{ continue; }
// 方法二,将文件分批读入缓冲区
byte[] data = new byte[];
int size = ;
ZipEntry entry = new ZipEntry(Path.GetFileName(file.Name));
entry.DateTime = (file.CreationTime > file.LastWriteTime ? file.LastWriteTime : file.CreationTime);
s.PutNextEntry(entry);
while (true)
{
size = fs.Read(data, , size);
if (size <= ) break;
s.Write(data, , size);
}
fs.Close();
file.Delete();
Thread.Sleep(SleepTimer);
}
}
finally
{
s.Finish();
s.Close();
}
}
#endregion #region 解密、解压缩文件
/// <summary>
/// 解压缩文件
/// </summary>
/// <param name="GzipFile">压缩包文件名</param>
/// <param name="targetPath">解压缩目标路径</param>
public static void Decompress(string GzipFile, string targetPath)
{
//string directoryName = Path.GetDirectoryName(targetPath + "\\") + "\\";
string directoryName = targetPath;
if (!Directory.Exists(directoryName)) Directory.CreateDirectory(directoryName);//生成解压目录
string CurrentDirectory = directoryName;
byte[] data = new byte[];
int size = ;
ZipEntry theEntry = null;
using (ZipInputStream s = new ZipInputStream(File.OpenRead(GzipFile)))
{
while ((theEntry = s.GetNextEntry()) != null)
{
if (theEntry.IsDirectory)
{// 该结点是目录
if (!Directory.Exists(CurrentDirectory + theEntry.Name)) Directory.CreateDirectory(CurrentDirectory + theEntry.Name);
}
else
{
if (theEntry.Name != String.Empty)
{
//解压文件到指定的目录
using (FileStream streamWriter = File.Create(CurrentDirectory + theEntry.Name))
{
while (true)
{
size = s.Read(data, , data.Length);
if (size <= ) break; streamWriter.Write(data, , size);
}
streamWriter.Close();
}
}
}
}
s.Close();
}
}
#endregion
}
}

压缩DLL https://pan.baidu.com/s/1kIgRcvvzB5ZybHITsRi4ZQ

HttpResponse输出文件的更多相关文章

  1. HttpRequest获取文件流,HttpResponse输出文件流

    HttpResponse输出文件: Response.Clear(); Response.ContentType = "application/octet-stream"; //通 ...

  2. 未能写入输出文件“c:\Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET Files\root\106f9ae8\cc0e1

    在本地开发环境没问题,但是发布到服务器出现:未能写入输出文件"c:\Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.Ne ...

  3. CS0016: 未能写入输出文件“c:\WINDOWS\Microsoft.NET\Framework\.。。”--“拒绝访问

    aspx 常见错误 CS0016: 未能写入输出文件“c:/WINDOWS/Microsoft.NET/Framework/v2.0.50727/Temporary ASP.NET Files/... ...

  4. C# iis 错误配置信息( CS0016: 未能写入输出文件 )

    IIS发布时,编译器错误消息: CS0016: 未能写入输出文件 "c:\Windows\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.N ...

  5. 转发 win7+iis7.5+asp.net下 CS0016: 未能写入输出文件“c:\Windows\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files 解决方案

    win7+iis7.5+asp.net下 CS0016: 未能写入输出文件“c:\Windows\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NE ...

  6. 在编译向该请求提供服务所需资源的过程中出现错误。请检查下列特定错误详细信息并适当地修改源代码。 编译器错误消息: CS0016: 未能写入输出文件“c:\Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET Files\root\41c191fd\ff9345c5\App_Web_login.cshtml.65793277

    本地开发环境没问题,但是发布到服务器出现问题或则直接在IIS上修改东西就给我抛出以下错误: 未能写入输出文件"c:\Windows\Microsoft.NET\Framework64 \v4 ...

  7. ls按时间排序输出文件列表

    文件转自:http://www.2cto.com/os/201303/197829.html ls按时间排序输出文件列表   首先,ls --help查看ls相关的与时间排序相关的参数:   > ...

  8. System.Web.HttpCompileException (0x80004005): (0): error CS0016: 未能写入输出文件

    重新系统后,iis asp.net站点老是出现: System.Web.HttpCompileException (0x80004005): (0): error CS0016: 未能写入输出文件“c ...

  9. Win7下:编译器错误信息: CS0016: 未能写入输出文件

    错误如下: "/"应用程序中的服务器错误. 编译错误 说明: 在编译向该请求提供服务所需资源的过程中出现错误.请检查下列特定错误详细信息并适当地修改源代码. 编译器错误消息: CS ...

随机推荐

  1. springboot2.x接口返回中文乱码

    @Configuration public class GlobalConversConfiguration extends WebMvcConfigurationSupport { @Bean pu ...

  2. SudaMod-81.0 / crDroidAndroid-8.1(android-8.1.0_r20)红米3 2018年5月3日更新

    一.写在前面 我只是个人爱好,本ROM未集成任何第三方推广软件,我只是喜欢把好的资源分享出来,若可以,我们一起学习,一起进步. 请不要问我怎么刷机! 请不要问我玩游戏卡不卡(有钱你就换好点的手机)! ...

  3. git踩过的坑

    一.git 解决fatal: Not a git repository 我用git add file添加文件时出现这样错误: fatal: Not a git repository (or any o ...

  4. 解决vscode无法安装golang相关插件的问题 - 即无法直连golang.org的问题

    喜欢挂vpn或者代理的请无视本文. 其实golang.org上的插件在github.com上都有镜像,直接 git clone https://github.com/golang/tools git ...

  5. python一个简单的websocket测试客户端

    朋友发的,之前在网上一直没找着,先记着 #!/usr/bin/env python import asyncio import websockets import json async def tes ...

  6. go对json对象的生成和解析

    https://blog.csdn.net/benben_2015/article/details/78917374

  7. MA5680T OLT管理软件,全智能判断板卡,无人值守策略,根据光猫类型自动扫描添加光猫

    轻量型的ONU查询工具,智能查询板卡,查询自动发现,搜索ONU等功能,需要更多智能功能,可联系QQ:561454825 下载地址:下载地址1

  8. Jquery EasyUI Combotree和 EasyUI tree展开所有父节点和获取完整路径

    Jquery EasyUI Combotree展开所有父节点 Jquery EasyUI Combotree获取树完整路径 Jquery EasyUI tree展开所有父节点 Jquery EasyU ...

  9. bootstrap 使用总结

    1.如何设置一行两列? <div class="container"> <div class="row"> <div class= ...

  10. gitlab+jenkins+tomcat CI/CD 部署

    整个项目的框架为: gitlab的安装与使用(Centos7) gitlab的安装 新建yum源 vim /etc/yum.repos.d/gitlab-ce.repo [gitlab-ce] nam ...