關於我

我的相片
用心思考、保持熱情,把工作上的經驗作分享與紀錄。希望能夠跟大家一起不斷的成長~

將數個檔案依指定名稱zip後,response回page讓client下載



protected void BtnSave_Click(object sender, EventArgs e)
{
MemoryStream ms = FEDIClientFacade.FEDISetupToExcel(this, DropDownList1.SelectedValue);
string txtFEDIName = "金融EDI交易量統計表" + (DateTime.Now.Year - 1911).ToString("000") + DateTime.Now.Month.ToString("00") + ".xls";

MemoryStream msCust = FEDIClientFacade.FEDISetupCustomerToExcel(this, DropDownList1.SelectedValue);
string txtFEDINameCust = "金融EDI客戶統計表" + (DateTime.Now.Year - 1911).ToString("000") + DateTime.Now.Month.ToString("00") + ".xls";

bool isSuccess = Utility.DoStreamZipPopUpDownload(this, new Stream[2] { ms, msCust },
new string[] { txtFEDIName, txtFEDINameCust }, "金融EDI統計表.zip");
if (!isSuccess)
ClientScript.RegisterStartupScript(this.GetType(), "NG", "<script>alert('報表產生失敗!')</script>");
}

//-----------------------------------------------------------------------------//

///
/// 將Stream依指定路徑與檔名存檔
///

/// Stream
/// 檔案完整路徑與檔名
public static void SaveStreamToFile(Stream stream, string filePathName)
{
string nPath = filePathName.Substring(0, filePathName.IndexOf(filePathName.Split('\\')[filePathName.Split('\\').Length - 1]));
if (!Directory.Exists(nPath))
Directory.CreateDirectory(nPath);
System.IO.File.WriteAllBytes(filePathName, StreamToByteArray(stream));
}

///
/// 將Stream轉為Byte[]
///

/// Stream
/// Byte[]
public static byte[] StreamToByteArray(Stream stream)
{
long FileSize = stream.Length;
byte[] Buffer = new byte[FileSize];
stream.Read(Buffer, 0, (int)FileSize);
stream.Close();
return Buffer;
}


///
/// 將數個檔案依指定名稱zip後,response回page讓client下載
///

/// popup之page
/// 各檔案完整路徑+名稱
/// zip檔案之名稱
///
public static bool DoZipPopUpDownload(System.Web.UI.Page page, string[] files, string zipname)
{
try
{
//建立暫存資料夾
string zipTempPath = "~/zipTemp/" + new Random().Next().ToString() + "/";
string zipTempPathOrg = zipTempPath + "org/";
if (!Directory.Exists(page.Server.MapPath(zipTempPathOrg)))
Directory.CreateDirectory(page.Server.MapPath(zipTempPathOrg));

//將檔案複製至暫存資料夾
foreach (string file in files)
{
string filename = file.Split('\\')[file.Split('\\').Length - 1];
//string filename = file.Substring(0, file.IndexOf(file.Split('\\')[file.Split('\\').Length - 1]));
File.Copy(file, page.Server.MapPath(zipTempPathOrg + filename));
}

//建立zip壓縮檔
ICSharpCode.SharpZipLib.Zip.FastZip fz = new ICSharpCode.SharpZipLib.Zip.FastZip();
fz.CreateZip(page.Server.MapPath(zipTempPath + zipname), page.Server.MapPath(zipTempPathOrg), true, "");

//Response下載zip檔
byte[] zip = File.ReadAllBytes(page.Server.MapPath(zipTempPath + zipname));
page.Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", page.Server.UrlPathEncode(zipname)));
page.Response.BinaryWrite(zip);

//清空檔案與暫存檔
Directory.Delete(page.Server.MapPath(zipTempPath), true);
return true;
}
catch
{
return false;
}
}


///
/// 將數個資料Stream依指定名稱zip後,response回page讓client下載
///

/// popup之page
/// 各檔案資料Stream
/// 對應各檔案Stream之檔案名稱
/// zip檔案之名稱
///
public static bool DoStreamZipPopUpDownload(System.Web.UI.Page page, Stream[] streams,string[] filenames, string zipname)
{
try
{
//建立暫存資料夾
string zipTempPath = "~/zipTemp/" + new Random().Next().ToString() + "/";
if (!Directory.Exists(page.Server.MapPath(zipTempPath)))
Directory.CreateDirectory(page.Server.MapPath(zipTempPath));

if (streams.Length == filenames.Length)
{
string[] files = new string[streams.Length];
//資料寫入暫存
for (int i = 0; i < streams.Length; i++)
{
SaveStreamToFile(streams[i], page.Server.MapPath(zipTempPath + filenames[i]));
files[i] = page.Server.MapPath(zipTempPath + filenames[i]);
}

//製作popup zip download
bool isSuccess = DoZipPopUpDownload(page, files, zipname);

//清空暫存
Directory.Delete(zipTempPath, true);
return isSuccess;
}
else
{
return false;
}

}
catch
{
return false;
}
}

沒有留言:

張貼留言