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

///   
    /// 文件下载  
    ///   
    /// 文件名  
    /// 路径  
    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(); 
        }
    }  

 

转载于:https://www.cnblogs.com/ly77461/p/6807251.html

你可能感兴趣的:(c#文件下载---以文件流形式)