用户点击超链接直接进入下载图片,文件等

前台添加<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);
        }




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值