asp.net 文件下载

                            //excelFile为文件在服务器上的地址
                            FileInfo fi = new FileInfo(localFilePath);
                            //服务器响应
                            HttpResponse contextResponse = HttpContext.Current.Response;
                            //检查文件是否已存有,有则报错
                            if (!System.IO.File.Exists(localFilePath))
                            {
                                Response.Write("<script language='javascript'>alert('对不起,文件不存在!');</script>");
                                return;
                            }
                            contextResponse.Clear();
                            contextResponse.Buffer = true;
                            //设置了类型为中文防止乱码的出现
                            contextResponse.Charset = "utf-8";
                            //文件名
                            string strfileName = (string)DbHelper.Nc2(drow, "ImportFileName");
                            //文件名
                            strfileName = HttpUtility.UrlEncode(strfileName, System.Text.Encoding.UTF8);
                            //去除文件名中间的“+”变空格
                            strfileName = strfileName . Replace("+", "%20");          --下载保存时文件名显示替换
                            //定义输出文件和文件名
                            contextResponse.AppendHeader("Content-Disposition", String.Format("attachment;filename={0}", strfileName)); //定义输出文件和文件名
                            //文件长度
                            contextResponse.AppendHeader("Content-Length", fi.Length.ToString());
                            //默认编码
                            contextResponse.ContentEncoding = Encoding.Default;
                            //设置输出文件类型为xls文件。
                            contextResponse.ContentType = "application/xls";
                            //下载到本地
                            contextResponse.WriteFile(fi.FullName .Replace("+"," "));          --下载打开时文件名显示替换
                            contextResponse.Flush();
                            contextResponse.Close();
                            contextResponse.End();
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值