如何调用浏览器文件框弹出下载按钮

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();

需要在程序中加入这样一段响应,就可以弹出框下载

相关多种下载方式:
https://www.cnblogs.com/bweb/p/4711412.html

1.流方式下载
///
/// 流方式下载文件不能超过400M
///
///
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();
}

2.TransmitFile实现下载
///
/// TransmitFile下载文件
///
/// 服务器相对路径
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();
        }

    }

3.WriteFile实现下载
///
/// 使用WriteFile下载文件
///
/// 相对路径
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();
}
}

4.WriteFile分块下载:
///
/// 使用OutputStream.Write分块下载文件
///
///
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 
        { }
        finally
        {
            if (stream != null)
            {
                stream.Close();
            }
            HttpContext.Current.Response.Close();
        }

    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值