Asp.net文档的下载,基于httpp协议实现

此方法能够实现,在网络环境下的asp.net编程实现的web应用。这里主要也是流式的实现,能够轻松应对各种网络环境的下载实现。

主要用到两个参数:(1)fileName:要下载文件在服务器端的物理路径

(2)page:调用本方法的页面的实例化对象。

针对其他语言基本实现大致相同。

//http文件下载
    public static void FileDownMethod(string fileName,Page page)
    {
        if (File.Exists(fileName))
        {
            FileInfo DownloadFile = new FileInfo(fileName);
            FileStream stream = new FileStream(fileName, FileMode.Open,
            FileAccess.Read, FileShare.Read);
            try
            {

                page.Response.Charset = "GB2312";
                page.Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
                page.Response.ContentType = "application/octet-stream";
                page.Response.AppendHeader("Content-Disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode(DownloadFile.Name));
                int bufSize = (int)stream.Length;
                byte[] buf = new byte[bufSize];
                int bytesRead = stream.Read(buf, 0, bufSize);
                page.Response.OutputStream.Write(buf, 0, bytesRead);
                page.Response.End();
            }
            finally
            {
                stream.Close();
            }
        }
        else
        {
            MyClientScript.ygJScript.Alert("服务器找不到该文件!", page);
        }
    }


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值