Chennairam
New Member
I have been trying to perform a simple upload operation of an image using the asp FileUpload control and save it in Oracle database. Though I have hardcoded a few lines, I somehow succeeded. But then, I really want to understand the issue and I seek experts opinion. After a lot of googling, I attempted using various code snippets in my web application and eventually failed. Here is the code that partially worked for me.\[code\]protected void btnSubmit_Click(object sender, EventArgs e) { String strFileName = Path.GetFileName(imgUpload.PostedFile.FileName); String strFileExtension = Path.GetExtension(imgUpload.PostedFile.FileName); byte[] byteArray = null; if (imgUpload.PostedFile != null) { using (FileStream fs = new FileStream(strFileName, FileMode.Open, FileAccess.Read, FileShare.Read)) { byteArray = new byte[fs.Length]; int iBytesRead = fs.Read(byteArray, 0, (int)fs.Length); } string sql = " INSERT INTO IMAGETBL(ID,IMAGE) VALUESID, :IMAGE) "; OracleConnection conn = new OracleConnection("Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=localhost)(PORT=1521)))(CONNECT_DATA=http://stackoverflow.com/questions/15708903/(SERVER=DEDICATED)(SERVER_NAME=XE)));User Id=sakthi_studdb;Password=sakthi;"); try { conn.Open(); OracleCommand cmd = new OracleCommand(sql, conn); cmd.Parameters.Add("ID", OracleDbType.Int32, 4, ParameterDirection.Input); cmd.Parameters.Add("IMAGE", OracleDbType.Blob, byteArray, ParameterDirection.Input); cmd.ExecuteNonQuery(); secondlabel.Text = "Image added to blob field"; } catch (Exception ex) { secondlabel.Text = ex.ToString(); } finally { conn.Close(); } } }\[/code\]When I run this code, it prompts Filenotfound error. Could not find file 'C:\Program Files\Common Files\Microsoft Shared\DevServer\10.0'Then to make it work I had to manually copy paste my image file in the above mentioned location. There must be a way to create a directory and save the images there temporarily. Later the same image should be inserted in Oracle database.\[code\] HttpPostedFile imgFile = imgUpload.PostedFile; int imgFileLength = imgFile.ContentLength; if (imgFileLength > 0) { var fileName = System.IO.Path.GetFileName(imgFile.FileName); var fileUpload = Path.Combine(Server.MapPath("~/user_uploads"), fileName); imgFile.SaveAs(fileUpload); if (System.IO.File.Exists(fileUpload)) { using (System.IO.StreamReader sr = new System.IO.StreamReader(fileUpload)) { var input = sr.ReadToEnd(); var lines = Regex.Split(input, "#!#");\[/code\]And another one..\[code\]string path = HttpContext.Current.ApplicationInstance.Server.MapPath("~/user_uploads"); string fn = System.IO.Path.GetFileName(imgUpload.PostedFile.FileName); imgUpload.PostedFile.SaveAs(System.IO.Path.Combine(path, fn));
Mylabel.Text = Path.GetFullPath(strFileName); imgUpload.PostedFile.SaveAs(strFileName);\[/code\]I hardly understood these snippets. Also, if you know easier methods to accomplish the same do suggest. Kindly help me out!
Mylabel.Text = Path.GetFullPath(strFileName); imgUpload.PostedFile.SaveAs(strFileName);\[/code\]I hardly understood these snippets. Also, if you know easier methods to accomplish the same do suggest. Kindly help me out!