前台添加<a href="hander.ashx">下载</a>
下载图片,文件等:
public void ProcessRequest (HttpContext context) {
context.Response.ContentType = "image/JPEG";
context.Response.AddHeader("Content-Disposition","attachment;filename=haha.jpg");//主要是这里,添加响应头文件
context.Response.WriteFile("zs.jpg");
}
下载Excel文件:
hander.ashx.cs文件中添加代码:
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "application/x-excel";//修改相应类型
context.Response.AddHeader("Content-Disposition", "attachment;filename=1.xls");//主要是这里,添加响应头文件
IWorkbook workbook = new HSSFWorkbook();//创建Workbook对象
ISheet sheet = workbook.CreateSheet();//创建工作表
string username = "renjing";
string password = "124";
IRow row = sheet.CreateRow(0);//在工作表中添加一行
ICell cell = row.CreateCell(0);//在行中添加一列
cell.SetCellValue(username);//设置列的内容
cell = row.CreateCell(1);//在行中添加一列
cell.SetCellValue(password);//设置列的内容
workbook.Write(context.Response.OutputStream);
}