使用FileStream文件流下载---源码

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;

/// <summary>
/// 文件下载
/// </summary>
public class DownLoad
{
    /// <summary>
    /// 文件下载类型
    /// </summary>
    public enum FileType {
       xls,
       doc,
       txt,
       zip
    }
    /// <summary>
    /// 文件下载
    /// </summary>
    /// <param name="FullFileName"></param>
    /// <param name="DisplayName"></param>
    /// <param name="fileType"></param>
    /// <param name="callPage"></param>
    public static void Run(string FullFileName,string DisplayName,FileType fileType, System.Web.UI.Page callPage)
    {
        try
        {
            FileStream fsZ = File.OpenRead(@FullFileName);
            byte[] bufferZ = new byte[fsZ.Length];
            fsZ.Read(bufferZ, 0, bufferZ.Length);
            fsZ.Close();
            callPage.Response.Clear();
            callPage.Response.Buffer = true;
            callPage.Response.Charset = "utf-8";
            switch (fileType)
            {
                case FileType.doc:
                    callPage.Response.ContentType = "application/ms-word";
                    DisplayName += ".doc";
                    break;
                case FileType.xls:
                    callPage.Response.ContentType = "application/vnd.ms-excel";
                    DisplayName += ".xls";
                    break;
                case FileType.txt:
                    callPage.Response.ContentType = "application/ms-txt";
                    DisplayName += ".txt";
                    break;
                case FileType.zip:
                    callPage.Response.ContentType = "application/zip";
                    DisplayName += ".zip";
                    break;
            }
            callPage.Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(@DisplayName.Trim(), System.Text.Encoding.UTF8));
            callPage.Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8");//设置输出流为简体中文
         
            callPage.Response.BinaryWrite(bufferZ);
            //callPage.Response.End(); 
        }
        catch (Exception ex)
        {
            throw new Exception(ex.Message);
        }

    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值