1. 程式人生 > >asp.net以字元流的形式下載檔案

asp.net以字元流的形式下載檔案

        string fileNameXZ = "orderlist.zip";//客戶端儲存的檔名
        string filePath = Server.MapPath("/createimg/orderList");//路徑

        //以字元流的形式下載檔案
        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";

        //通知瀏覽器下載檔案而不是開啟
        Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileNameXZ, System.Text.Encoding.UTF8));
        Response.BinaryWrite(bytes);
        Response.Flush();
        Response.End();