En seguida el listado del código
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.IO;
using Oracle.DataAccess.Client;
using Oracle.DataAccess.Types;
using System.Configuration;
namespace ResourceAccess.Oracle
{
/// <summary>
///
/// </summary>
public class FileUploader
{
#region Private Members
public const int BUFFER_SIZE = 1024;
private string _tableName = "DOCUMENTCONTENT";
private string _idField = "ID";
private string _streamField = "STREAM";
private string _lastUpdateField = "LASTUPDATE";
private string _fileNameField = "FILENAME";
private string _connectionString;
private List<string> _filesToUpload;
byte[] uploadBuffer;
OracleConnection oracle;
OracleTransaction transaction;
#endregion
#region Properties
/// <summary>
///
/// </summary>
public List<string> FilesToUpload
{
get {
if (_filesToUpload == null)
_filesToUpload = new List<string>();
return _filesToUpload; }
set { _filesToUpload = value; }
}
/// <summary>
///
/// </summary>
public string ConnectionString
{
get { return _connectionString; }
set { _connectionString = value; }
}
/// <summary>
///
/// </summary>
public string UploadCommand
{
get {
return
"INSERT INTO "+_tableName+" ("+_idField +", "+_streamField +", "+_lastUpdateField +","+_fileNameField +") VALUES (sys_guid(), :blob, sysdate, :filename)";
}
}
/// <summary>
///
/// </summary>
public string TableName
{
get { return _tableName; }
set { _tableName = value; }
}
/// <summary>
///
/// </summary>
public string IdField
{
get { return _idField; }
set { _idField = value; }
}
/// <summary>
///
/// </summary>
public string LastUpdateField
{
get { return _lastUpdateField; }
set { _lastUpdateField = value; }
}
/// <summary>
///
/// </summary>
public string FileNameField
{
get { return _fileNameField; }
set { _fileNameField = value; }
}
#endregion
#region Constructors
/// <summary>
///
/// </summary>
public FileUploader() {
_connectionString = ConfigurationManager.ConnectionStrings["DBStorage"].ConnectionString;
}
/// <summary>
///
/// </summary>
/// <param name="connectionString"></param>
public FileUploader(string connectionString)
{
_connectionString = connectionString;
}
#endregion
#region Public methods
/// <summary>
///
/// </summary>
/// <returns></returns>
public DataTable ListAllFiles() {
try
{
oracle = new OracleConnection(ConnectionString);
oracle.Open();
OracleCommand command = new OracleCommand(UploadCommand, oracle);
command.CommandType = CommandType.Text;
command.CommandText = "SELECT " + _idField + ", " + _fileNameField + ", " + _lastUpdateField + " FROM " + _tableName;
DataTable dt = new DataTable();
OracleDataAdapter da = new OracleDataAdapter(command);
da.Fill(dt);
oracle.Close();
oracle.Dispose();
return dt;
}
catch (Exception ex)
{
if (oracle != null)
{
if (oracle.State != ConnectionState.Closed)
{
oracle.Close();
oracle.Dispose();
}
}
throw ex;
}
return null;
}
/// <summary>
///
/// </summary>
public bool UploadFile(Stream fileStream, string filename)
{
try
{
oracle = new OracleConnection(ConnectionString);
oracle.Open();
uploadBuffer = new byte[fileStream.Length];
fileStream.Read(uploadBuffer, 0, (int)fileStream.Length);
fileStream.Close();
UploadFile(filename);
oracle.Close();
oracle.Dispose();
return true;
}
catch (Exception ex)
{
if (oracle != null)
{
if (oracle.State != ConnectionState.Closed)
{
oracle.Close();
oracle.Dispose();
}
}
throw ex;
}
return false;
}
/// <summary>
///
/// </summary>
public void UploadFiles() {
try
{
if (FilesToUpload.Count > 0)
{
oracle = new OracleConnection(ConnectionString);
oracle.Open();
foreach (string filepath in FilesToUpload)
{
if (System.IO.File.Exists(filepath))
{
Stream fileStream = File.OpenRead(filepath);
uploadBuffer = new byte[fileStream.Length];
fileStream.Read(uploadBuffer, 0, (int)fileStream.Length);
fileStream.Close();
UploadFile(filepath);
}
}
oracle.Close();
}
}
catch (Exception ex) {
if (oracle != null) {
if (oracle.State != ConnectionState.Closed) {
oracle.Close();
}
}
throw ex;
}
}
/// <summary>
///
/// </summary>
/// <param name="ID"></param>
/// <returns></returns>
public Byte[] DownloadFile(string ID)
{
string sql = "SELECT * FROM "+TableName+" WHERE ID = :ID AND ROWNUM<=1";
Byte[] byteArray =null;
using (OracleConnection conn = new OracleConnection(_connectionString))
{
conn.Open();
using (OracleCommand cmd = new OracleCommand(sql, conn))
{
cmd.Parameters.Add (new OracleParameter (_idField, ID));
using (IDataReader dataReader = cmd.ExecuteReader())
{
while (dataReader.Read())
{
byteArray = (Byte[])dataReader[_streamField];
}
}
}
}
return byteArray;
}
/// <summary>
///
/// </summary>
/// <param name="ID"></param>
/// <returns></returns>
public string DownloadFile(string ID, string rootPath)
{
string sql = "SELECT * FROM " + TableName + " WHERE ID = :ID AND ROWNUM<=1";
string filepath=string.Empty ;
using (OracleConnection conn = new OracleConnection(_connectionString))
{
conn.Open();
using (OracleCommand cmd = new OracleCommand(sql, conn))
{
cmd.Parameters.Add(new OracleParameter(_idField, ID));
using (IDataReader dataReader = cmd.ExecuteReader())
{
while (dataReader.Read())
{
Byte[] byteArray = (Byte[])dataReader[_streamField];
filepath = rootPath + @"\" + dataReader[_fileNameField].ToString();
using (FileStream fs = new FileStream(filepath, FileMode.CreateNew, FileAccess.Write ))
{
fs.Write(byteArray, 0, byteArray.Length);
}
}
}
}
}
return filepath;
}
/// <summary>
///
/// </summary>
/// <returns></returns>
public List<Byte[]> DownloadAllFiles()
{
string sql = "SELECT * FROM " + TableName ;
List<Byte[]> files=new List<byte[]> ();
using (OracleConnection conn = new OracleConnection(_connectionString))
{
conn.Open();
using (OracleCommand cmd = new OracleCommand(sql, conn))
{
using (IDataReader dataReader = cmd.ExecuteReader())
{
while (dataReader.Read())
{
Byte[] byteArray = (Byte[])dataReader[_streamField];
files.Add(byteArray);
}
}
}
}
return files;
}
/// <summary>
///
/// </summary>
/// <returns></returns>
public List<string> DownloadAllFiles(string rootPath)
{
List<string> downloadedfiles=new List<string> ();
string sql = "SELECT * FROM " + TableName;
Byte[] byteArray = null;
string filepath;
using (OracleConnection conn = new OracleConnection(_connectionString))
{
conn.Open();
using (OracleCommand cmd = new OracleCommand(sql, conn))
{
using (IDataReader dataReader = cmd.ExecuteReader())
{
while (dataReader.Read())
{
byteArray = (Byte[])dataReader[_streamField];
filepath = rootPath + @"\" + dataReader[_fileNameField].ToString();
using (FileStream fs = new FileStream(filepath, FileMode.CreateNew, FileAccess.Write))
{
fs.Write(byteArray, 0, byteArray.Length);
downloadedfiles.Add(filepath);
}
}
}
}
}
return downloadedfiles;
}
#endregion
#region Private Methods
/// <summary>
///
/// </summary>
/// <param name="filePath"></param>
private void UploadFile(string filePath) {
transaction = oracle.BeginTransaction();
OracleCommand command = new OracleCommand(UploadCommand, oracle);
OracleParameter uploadParameter = new OracleParameter(":blob", OracleDbType.Blob, ParameterDirection.Input);
OracleParameter filenameParameter = new OracleParameter(":filename", OracleDbType.Varchar2 , ParameterDirection.Input);
OracleBlob blob = new OracleBlob(oracle);
int startOffset = 0;
int writeBytes = 0;
/*
* ... now load the buffer in small chunks to Oracle
*/
blob.BeginChunkWrite();
do
{
writeBytes = startOffset + BUFFER_SIZE > uploadBuffer.Length ? uploadBuffer.Length - startOffset : BUFFER_SIZE;
blob.Write(uploadBuffer, startOffset, writeBytes);
startOffset += writeBytes;
} while (startOffset < uploadBuffer.Length);
blob.EndChunkWrite();
uploadParameter.Value = blob;
filenameParameter.Value = System.IO.Path.GetFileName(filePath);
command.Parameters.Add(uploadParameter);
command.Parameters.Add(filenameParameter);
command.ExecuteNonQuery();
transaction.Commit();
}
#endregion
}
}