private void downLoad(string id)
{
string fileName = Page.Request.PhysicalApplicationPath + "SystemManage\\SysFile\\" + DateTime.Now.ToString("yyyyMMddhhmmss") + ".zip";
OracleConnection conn = null;
string connString = System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"].ToString();
using (conn = new OracleConnection(connString))
{
try
{
conn.Open();
OracleCommand cmd = conn.CreateCommand();
// 利用交易處理(必須)
OracleTransaction transaction = cmd.Connection.BeginTransaction();
cmd.Transaction = transaction;
// 根據查詢語句擷取對應的上傳檔案的BLOB資訊
string sql = "select 上傳檔案 from 檔案上傳表 where 編號 = " + id;
cmd.CommandText = sql;
OracleDataReader dr = cmd.ExecuteReader();
dr.Read();
OracleLob tempLob = dr.GetOracleLob(0);
dr.Close();
// 讀取 BLOB 中資料,寫入到檔案中
FileStream fs = new FileStream(fileName, FileMode.Create);
int length = 1048576;
byte[] Buffer = new byte[length];
int i;
while ((i = tempLob.Read(Buffer, 0, length)) > 0)
{
fs.Write(Buffer, 0, i);
}
fs.Close();
tempLob.Clone();
cmd.Parameters.Clear();
// 提交事務
transaction.Commit();
DownloadFile(fileName);
File.Delete(fileName);
}
catch (Exception ex)
{
throw ex;
}
finally
{
conn.Close();
}
}
}
/// <summary>
/// 下載檔案
/// </summary>
/// <param name="fileName">檔案名稱</param>
public void DownloadFile(string fileName)
{
try
{
//by K 2010-08-13 下載超過100M的附件 需要用以下方法
System.IO.Stream iStream = null;
// Buffer to read 10K bytes in chunk:
byte[] buffer = new Byte[10000];
// Length of the file:
int length;
// Total bytes to read:
long dataToRead;
// Identify the file to download including its path.
string filepath = fileName;
// Identify the file name.
string filename = System.IO.Path.GetFileName(filepath);
try
{
// Open the file.
iStream = new System.IO.FileStream(filepath, System.IO.FileMode.Open,
System.IO.FileAccess.Read, System.IO.FileShare.Read);
// Total bytes to read:
dataToRead = iStream.Length;
Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Disposition", "attachment; filename=" + filename);
// Read the bytes.
while (dataToRead > 0)
{
// Verify that the client is connected.
if (Response.IsClientConnected)
{
// Read the data in buffer.
length = iStream.Read(buffer, 0, 10000);
// Write the data to the current output stream.
Response.OutputStream.Write(buffer, 0, length);
// Flush the data to the HTML output.
Response.Flush();
buffer = new Byte[10000];
dataToRead = dataToRead - length;
}
else
{
//prevent infinite loop if user disconnects
dataToRead = -1;
}
}
}
catch (Exception ex)
{
// Trap the error, if any.
Response.Write("Error : " + ex.Message);
}
finally
{
if (iStream != null)
{
//Close the file.
iStream.Close();
}
}
}
catch (Exception ex)
{
AppCode.CommonFunc.AlertScript("對不起,檔案下載時出現錯誤!");
}
}