c#文件下载---以文件流形式

/// <summary>  
    /// 文件下载  
    /// </summary>  
    /// <param name="FileName">文件名</param>  
    /// <param name="FilePath">路径</param>  
    public void BigFileDownload(string FileName, string FilePath)
    {
        try
        {

            //以字符流的形式下载文件
            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;charset=gb2321";

            //通知浏览器下载文件而不是打开;对中文名称进行编码
            Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(FileName, System.Text.Encoding.UTF8));
            Response.BinaryWrite(bytes);
            Response.Flush();
            Response.End(); 
        }
        catch (Exception ex)
        {
            
        }
        finally
        {
            Response.Flush();
            Response.End(); 
        }
    }  

 

posted @ 2017-05-04 15:11  ly77461  阅读(7251)  评论(0编辑  收藏  举报