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(); 
        }
    }  
相關文章
相關標籤/搜索