c# 浏览器弹出提示框下载文件

c#下载文件四种方法:

1. 流方式下载

2.TransmitFile实现下载

3.WriteFile实现下载

4.WriteFile分块下载

/// <summary>
/// 流方式下载文件不能超过400M
/// </summary>
/// <param name="filePath">服务器相对路径</param>
public void RenderToBrowser(string filePath)
{
filePath = Server.MapPath(filePath);//路径
//以字符流的形式下载文件
FileStream fs = new FileStream(filePath, FileMode.Open);
byte[] bytes = new byte[(int)fs.Length];
fs.Read(bytes, 0, bytes.Length);
fs.Close();
Response.ContentType = "application/octet-stream";
//文件名+文件格式 (这里编码采用的是utf-8)
Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode("文件名.txt", System.Text.Encoding.UTF8));
Response.BinaryWrite(bytes);
Response.Flush();
Response.End();
}

/// <summary>
/// TransmitFile下载文件
/// </summary>
/// <param name="filePath">服务器相对路径</param>
public void TransmitFile(string filePath)
{
try
{
filePath = Server.MapPath(filePath);
if (File.Exists(filePath))
{
FileInfo info = new FileInfo(filePath);
long fileSize = info.Length;
HttpContext.Current.Response.Clear();

//指定Http Mime格式为压缩包
HttpContext.Current.Response.ContentType = "application/x-zip-compressed";

// Http 协议中有专门的指令来告知浏览器, 本次响应的是一个需要下载的文件. 格式如下:
// Content-Disposition: attachment;filename=filename.txt
//客户端保存的文件名 info.Name(例如 aaa.txt) 编码格式为utf-8
HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(info.Name, System.Text.Encoding.UTF8));

//不指明Content-Length用Flush的话不会显示下载进度
HttpContext.Current.Response.AddHeader("Content-Length", fileSize.ToString());
HttpContext.Current.Response.TransmitFile(filePath, 0, fileSize);
HttpContext.Current.Response.Flush();
}
}
catch
{ }
finally
{
HttpContext.Current.Response.Close();
}

}

/// <summary>
/// 使用WriteFile下载文件
/// </summary>
/// <param name="filePath">服务器相对路径</param>
public void WriteFile(string filePath)
{
try
{
filePath = Server.MapPath(filePath);
if (File.Exists(filePath))
{
FileInfo info = new FileInfo(filePath);
long fileSize = info.Length;
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.ContentType = "application/octet-stream";
HttpContext.Current.Response.AddHeader("Content-Disposition", "attachement;filename=" + HttpUtility.UrlEncode(info.Name, System.Text.Encoding.UTF8));
//指定文件大小
HttpContext.Current.Response.AddHeader("Content-Length", fileSize.ToString());
HttpContext.Current.Response.WriteFile(filePath, 0, fileSize);
HttpContext.Current.Response.Flush();
}
}
catch
{ }
finally
{
HttpContext.Current.Response.Close();
}
}

/// <summary>
/// 使用OutputStream.Write分块下载文件 
/// </summary>
/// <param name="filePath">服务器相对路径</param>
public void WriteFileBlock(string filePath)
{
filePath = Server.MapPath(filePath);
if (!File.Exists(filePath))
{
return;
}
FileInfo info = new FileInfo(filePath);
//指定块大小 
long chunkSize = 4096;
//建立一个4K的缓冲区 
byte[] buffer = new byte[chunkSize];
//剩余的字节数 
long dataToRead = 0;
FileStream stream = null;
try
{
//打开文件 
stream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read);

dataToRead = stream.Length;

//添加Http头 
HttpContext.Current.Response.ContentType = "application/octet-stream";
HttpContext.Current.Response.AddHeader("Content-Disposition", "attachement;filename=" + HttpUtility.UrlEncode(info.Name, System.Text.Encoding.UTF8));
HttpContext.Current.Response.AddHeader("Content-Length", dataToRead.ToString());

while (dataToRead > 0)
{
if (HttpContext.Current.Response.IsClientConnected)
{
//public abstract int Read(byte[] buffer, int offset, int count)
//buffer缓冲区;offset:缓冲区存储数据的开始位置 count: 从流中最多读取的字节数
int length = stream.Read(buffer,0, Convert.ToInt32(chunkSize));
HttpContext.Current.Response.OutputStream.Write(buffer, 0, length);
HttpContext.Current.Response.Flush();
HttpContext.Current.Response.Clear();
dataToRead -= length;
}
else
{
//防止client失去连接 
dataToRead = -1;
}
}
}
catch (Exception ex)
{
HttpContext.Current.Response.Write("Error:" + ex.Message);
}
finally
{
if (stream != null)
{
stream.Close();
}
HttpContext.Current.Response.Close();
}

}

时间: 2024-08-30 06:13:39

c# 浏览器弹出提示框下载文件的相关文章

基于Jquery 简单实用的弹出提示框

引言: 原生的 alert 样子看起来很粗暴,网上也有一大堆相关的插件,但是基本上都是大而全,仅仅几句话可以实现的东西,可能要引入好几十k的文件,所以话了点时间自己写了个弹出效果,放到项目上去发现效果还不错,这里贴出来,做个备忘,有需要的同学可以拿去,也可以作为一个参考. 1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.d

Android 底部弹出提示框的解决办法(使用Activity以及PopupWindow)

本片文章主要谈探讨了如何实现在底部弹出提示框背景为半透明效果的实现.想要实现此种效果一般有两种方式一个是使用Activity设置Theme另一种方式就是使用PopupWindow设置样式实现效果. 一,使用Activity 首先是此activity的布局文件: <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.andro

jQuery - 选中复选框则弹出提示框

<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8"> <title>判断是否选中</title> <script type="text/javascript" src="./js/jquery-1.8.3.min.js"></script> <script

如何实现android蓝牙开发 自动配对连接,并不弹出提示框

如何实现android蓝牙开发 自动配对连接,并不弹出提示框 之前做一个android版的蓝牙,遇到最大的难题就是自动配对. 上网查资料说是用反射createBond()和setPin(),但测试时进行配对还是会出现提示,但配对是成功了 我就开始查找怎么关闭这个蓝牙配对提示框,后面还是伟大的android源码帮助了我. 在源码 BluetoothDevice 类中还有两个隐藏方法 cancelBondProcess()和cancelPairingUserInput() 这两个方法一个是取消配对进

asp.net 后台代码跳转页面前弹出提示框

1.Response.Write("<script>alert('查询语句执行出错!');window.location.href=DisplayData.aspx</script>"); 2.Page.RegisterStartupScript("msg", "<script>alert('查询语句执行出错!');window.location.href='DisplayData.aspx'</script>

经验总结:WebBrowser自动点击弹出提示框alert、弹出对话框confirm、屏蔽弹出框、屏蔽弹出脚本错误的解决办法

经验总结:WebBrowser自动点击弹出提示框alert.弹出对话框confirm.屏蔽弹出框.屏蔽弹出脚本错误的解决办法 网上有好多解决方法,可是不一定好使,本人经过多次试验,针对WebBrowser控件中自动点击弹出框及禁用脚本提示问题得到如下几种实际情况的解决办法,绝对管用. 1.屏蔽弹出错误脚本 将WebBrowser控件ScriptErrorsSuppressed设置为True即可. (参考本篇博客:http://www.cnblogs.com/qqflying/archive/20

[转] 在Asp.net前台和后台弹出提示框

一.在前台弹出提示框 1.点击“A”标记或者“控件按钮”弹出提示框 <asp:LinkButton ID="lbtnDel" runat="server" OnClientClick='<%# "if(!confirm("你确定退订吗?"))return false;"%>' Text="删除"/> 2.方法二: <asp:LinkButton ID="lbtnDel

鼠标悬浮在超链接上弹出提示框

鼠标悬浮在超链接上弹出提示框:大家知道超链接有一个title属性,当鼠标放在链接的时候,可以出现一个提示框效果,不过自带的效果虽然廉价但往往并不物美,所以需要自定义一个,下面是一个纯CSS实现的这样的效果,和大家分享一下.代码实例如下: <!DOCTYPE html> <html> <head> <meta charset=" utf-8"> <meta name="author" content="h

js弹出提示框并跳转页面

1.提示框有两个选择项,点确定跳转,取消停留在原页面ScriptManager.RegisterStartupScript(Page, this.GetType(), "", "<script>if(confirm('请登录?')){location.href='login.aspx'};</script>", false); 2.提示框只有一个确定按钮,跳转到指定页面ScriptManager.RegisterStartupScript(p