下載ORACLE中BLOB內容到用戶端

來源:互聯網
上載者:User

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("對不起,檔案下載時出現錯誤!");
            }
        }

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.