博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
下载ORACLE中BLOB内容到客户端
阅读量:6848 次
发布时间:2019-06-26

本文共 2719 字,大约阅读时间需要 9 分钟。

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("对不起,文件下载时出现错误!");
            }
        }

转载地址:http://sxoul.baihongyu.com/

你可能感兴趣的文章
C# 如何在Excel表格中插入、编辑和删除批注
查看>>
基于微服务的电商系统架构
查看>>
用户管理及授权管理
查看>>
mysql 配置MHA
查看>>
Windows Developer Day - MSIX and Advanced Installer
查看>>
【tp5】ThinkCMF5框架,配置使其支持不同终端PC/WAP/Wechat能加载不同配置和视图
查看>>
spring security+freemarker获取登陆用户的信息
查看>>
[RxJS] Implement RxJS `concatMap` by Waiting for Inner Subscriptions to Complete
查看>>
ubuntu创建idea桌面快捷方式
查看>>
详解JNDI的lookup资源引用java:/comp/env
查看>>
如何在IntelliJ IDEA中使用Git .ignore插件忽略不必要提交的文件
查看>>
愿你走出半生,归来仍是Java Parser
查看>>
C# 获取接口数据(xml格式)转为json格式
查看>>
asp.net mvc session锁问题 (转载)
查看>>
[转]决定人生的三种成本:机会成本,沉没成本,边际成本
查看>>
A Generic Particle IO Library
查看>>
Enterprise Library 系列教程
查看>>
windows下搭建iphone开发环境
查看>>
关于信号量sem_wait的整理(转)
查看>>
MVC 3 数据验证 Model Validation 详解
查看>>