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("对不起,文件下载时出现错误!");
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构