Stream files into Oracle as Blob
505045Apr 12 2006 — edited Apr 12 2006I streamed the file into the Oracle database as a blob and when I got the Blob later it appeared to be incoded wrong; however, I think I have a fileupload problem and not a file download problem. I pass the optional 10 files into a object[] array, create the nessecary INSERT string and add the object[] converted to a byte[] array as the optional 10 params. I think its the coversion from the object[] array to the byte array that is messing up the encoding for the BLOB. I dont think its ODP.NET, but if you heard a rumor about the Stream a byte array to the BLOB problem let me know, so I can replace with SQL Server. Any Help on this matter is greatly appreciated.
public void lnbSend_Click(object s, EventArgs e)
{
object[] objStream = new object[getLength()];
string[] sarFileName = new string[getLength()];
int filesize = txtBody.Text.Length + txtSubject.Text.Length;
string strOra = System.Configuration.ConfigurationManager.AppSettings["OraString"];
OracleConnection con = new OracleConnection(strOra);
//OracleTransaction tran = new OracleTransaction();
try
{
string attach = "N";
for (int i = 1; i <= 10; i++)
{
HtmlInputCheckBox chk = (HtmlInputCheckBox)this.FindControl("ck" + i);
if (chk.Checked)
{
attach = "Y";
bool good = false;
FileUpload filAttach = (FileUpload)this.FindControl("filAttach" + i);
string FileType = filAttach.PostedFile.ContentType;
//Response.Write(FileType + "</br>");
string[] sta = {"application/x-zip-compressed", "image/bmp", "image/pjpeg", "image/gif", "text/plain", "application/octet-stream", "text/xml", "application/msword" };
for (int t = 0; t <= sta.Length - 1; ++t)
{
if (sta[t] == FileType)
{
good = true;
}
}
if (good == true)
{
byte[] file = new byte[filAttach.PostedFile.ContentLength];
Stream stm = filAttach.PostedFile.InputStream;
stm.BeginRead(file, 0, filAttach.PostedFile.ContentLength, null, null);
byte[] FileByte = new byte[filAttach.PostedFile.ContentLength];
sarFileName[i - 1] = System.IO.Path.GetFileName(filAttach.PostedFile.FileName);
filesize += filAttach.PostedFile.ContentLength;
objStream[i - 1] = FileByte;
/* byte[] file = new byte[filAttach.PostedFile.ContentLength];
Stream stm = filAttach.PostedFile.InputStream;
stm.BeginRead(file, 0, filAttach.PostedFile.ContentLength, null, null);
byte[] FileByte = new byte[filAttach1.PostedFile.ContentLength];
sarFileName[i - 1] = System.IO.Path.GetFileName(filAttach.PostedFile.FileName);
filesize += filAttach.PostedFile.ContentLength;
objStream[i - 1] = FileByte;
// clean garbage chars from byte array
for (int g = 0; g < FileByte.Length; g++)
{
FileByte[g] = 0;
}*/
}
}
}
Response.Write(Math.Round((double.Parse(filesize + "") / 1024), 2) + "");
//Response.Write(hdPriority.Value + "||");
//Get To Addr
Regex reg = new Regex("(.*?),");
string str = reg.Replace(txtTo.Text, "$1 ");
string mailid = "";
string[] arrTo = str.Split();
//open for once for all the To Addr
con.Open();
foreach (string strTo in arrTo)
{
OracleCommand cmd = new OracleCommand("SP_MAILID", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("p_email", strTo.ToUpper());
cmd.Parameters.Add("p_sender", "DOMINIC@DMAIL.COM"); //Local Mail-e
cmd.Parameters.Add("p_subject", txtSubject.Text);
cmd.Parameters.Add("p_body", txtBody.Text);
cmd.Parameters.Add("p_priority", hdPriority.Value);
cmd.Parameters.Add("p_attachment", attach);
cmd.Parameters.Add("p_message_size", Math.Round((double.Parse(filesize + "") / 1024), 2));
OracleParameter parm = cmd.Parameters.Add("p_mailid", OracleDbType.Int64);
parm.Direction = ParameterDirection.Output;
cmd.ExecuteNonQuery();
mailid = cmd.Parameters["p_mailid"].Value.ToString();
if (getLength() > 0)
{
string strColumns = "";
string strValues = "";
for (int i = 1; i <= objStream.Length; i++)
{
strColumns += "attach" + i + ", file_name" + i + ",";
strValues += ":attach" + i + ", :file_name" + i + ",";
}
strColumns = strColumns.Substring(0, strColumns.Length - 1) + ")";
strValues = strValues.Substring(0, strValues.Length - 1) + ")";
OracleCommand cmdA = new OracleCommand("", con);
cmdA.CommandText = "INSERT INTO attachment (mailid, " + strColumns + " values (" + mailid + ", " + strValues;
for (int i = 1; i <= objStream.Length; i++)
{
cmdA.Parameters.Add("attach" + i, (byte[])objStream[i - 1]);
cmdA.Parameters.Add("file_name" + i, sarFileName[i - 1]);
}
//con.Open();
cmdA.ExecuteNonQuery();
//con.Close();
}
}
con.Close();
//int test = getLength();
//Response.Write(test + "");
}
catch (Exception ex)
{
Response.Write(ex + "");
}
finally
{
con.Close();
}
Server.Transfer("Inbox.aspx");
}