Download blob content from Oracle to the client

Source: Internet
Author: 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. etettings ["connectionstring"]. tostring ();
Using (conn = new oracleconnection (connstring ))
{
Try
{
Conn. open ();
Oraclecommand cmd = conn. createcommand ();

 

// Use transaction processing (required)
Oracletransaction transaction = cmd. Connection. begintransaction ();
Cmd. Transaction = transaction;

// Obtain the Blob information of the uploaded file based on the query statement.
String SQL = "Select Upload file from File Upload table where no. =" + ID;
Cmd. commandtext = SQL;
Oracledatareader DR = cmd. executereader ();
Dr. Read ();
Required lelob templob = dr. get1_lelob (0 );
Dr. Close ();

// Read BLOB data and write it to the file
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 ();

// Submit the transaction
Transaction. Commit ();
Downloadfile (filename );
File. Delete (filename );
}
Catch (exception ex)
{
Throw ex;
}
Finally
{
Conn. Close ();
}
}
}

 

 

 

/// <Summary>
/// Download an object
/// </Summary>
/// <Param name = "FILENAME"> file name </param>
Public void downloadfile (string filename)
{
Try
{
// By K 2010-08-13 use the following method to download attachments larger than MB
System. Io. Stream istream = NULL;

 

// Buffer to read 10 K 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 ("sorry, an error occurred while downloading the file! ");
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.