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;
}
}
網誌標籤
.NET Framework
(5)
文字編碼
(4)
防毒軟體
(1)
其他
(11)
資料庫
(25)
攝影
(2)
ADO.NET Entity Framework
(2)
ASP.NET
(49)
ASP.NET MVC
(4)
ASP.NET Test
(4)
CSS
(1)
Design Patten
(1)
FCKEditor
(3)
HTML
(2)
HTML5
(1)
hyper-v
(1)
IIS
(6)
JavaScript
(9)
JQuery
(1)
LINQ
(2)
MailTo編碼
(2)
oracle
(6)
RegularExpression
(1)
Rhino Mocks
(1)
Security
(2)
Server2008R2
(1)
SharePoint
(5)
Sql 2008
(1)
SSIS
(1)
SyntaxHighlighter
(1)
TFS2010
(1)
VirtualBox
(1)
VistualStudio2010
(3)
vs2003升級vs2008
(1)
windows
(1)
windows 7
(1)
將數個檔案依指定名稱zip後,response回page讓client下載
訂閱:
張貼留言 (Atom)
沒有留言:
張貼留言