1

Following is the code to upload file in party_images table.

protected void upload_Click(object sender, EventArgs e)
{
    try
    {
        using (OracleConnection connection = new OracleConnection(conString))
        {
            connection.Open();
            string filename = Path.GetFileName(FileUpload1.FileName);
            string[] tokenize = filename.Split('.');
            FileUpload1.SaveAs(Server.MapPath("~/files/") + descBox.Text + "." + tokenize[1]);

            string sourceLoc = Server.MapPath("~/files/" + descBox.Text + "." + tokenize[1]);
            FileStream fs = new FileStream(sourceLoc, FileMode.Open, FileAccess.Read);
            byte[] ImageData = new byte[fs.Length];
            fs.Read(ImageData, 0, System.Convert.ToInt32(fs.Length));
            fs.Close();
            String block = " BEGIN " +
                           " INSERT INTO party_images(party_id, sr_no, descr, party_image) VALUES ('"+Session["userId"]+"',"+srNo.Text+",'"+descBox.Text+"."+tokenize[1]+"', :1); " +
                           " END; ";
            OracleCommand cmd = new OracleCommand();
            cmd.CommandText = block;
            cmd.Connection = connection;
            cmd.CommandType = CommandType.Text;
            OracleParameter param = cmd.Parameters.Add("blobtodb", OracleDbType.LongRaw);
            param.Direction = ParameterDirection.Input;
            param.Value = ImageData;
            cmd.ExecuteNonQuery();
            descBox.Text = "";
            srNo.Text = "";
        }
    }
    catch (Exception ex) {
            ClientScript.RegisterStartupScript(this.GetType(), "unSuccessMessage", "window.onload = function(){ alert('"+ex.Message+"')};", true);     
    }
    finally
    {
        populateGrid(loadFromDb());
    }
}

table description is,

PARTY_ID is VARCHAR2(10)

SR_NO is NUMBER

DESCR is VARCHAR2(50)

PARTY_IMAGE is LONG RAW()

This function is uploading all the files i.e., images,.docx,.pdf,.sql but when I upload any .docx containing screen shots or pictures then the upper error appears.

I have tried the following links,

ORA-01460: unimplemented or unreasonable conversion requested

The requested format conversion is not supported.

ORA-01460: unimplemented or unreasonable conversion requested

But I haven't got any solution. How can I upload any type of file without having this error?

Farrukh Sarmad
  • 315
  • 2
  • 5
  • 24
  • 1
    The very first thing to do is stop building SQL like that. Use parameterized SQL for everything. That may or may not be all that's required, but it's something you should do *immediately* to address the SQL Injection vulnerability you have at the moment. Additionally, I'd encourage you to use `File.ReadAllBytes` to load the data - at the moment you're *assuming* that a single call to `Read` will read all the data, which is generally a dangerous assumption for streams. – Jon Skeet Mar 15 '18 at 07:43
  • (Do you actually need to save the file at all? Can't you just use the data that's in `FileUpload1` without saving it as a file?) – Jon Skeet Mar 15 '18 at 07:45
  • If I will not save it then how will I read it in bytes as I have done. As I don't have the path of client side. @JonSkeet – Farrukh Sarmad Mar 15 '18 at 07:51
  • You'd just use the `FileBytes` property directly. – Jon Skeet Mar 15 '18 at 07:57
  • `byte[] ImageData = File.ReadAllBytes(sourceLoc);` i replaced Read with this but same error @JonSkeet – Farrukh Sarmad Mar 15 '18 at 08:04
  • Yes, I dare say it gives the same error, but it's all a matter of improving the code. But as I say, I'd remove all of the file access anyway, and just use `FileBytes` unless you actually need the file on the server as well. I still don't expect it to fix the problem, but you should do it anyway. Next, I'd try to reproduce this in a small console app, which you can post as a [mcve]. That probably *would* load a file, but you'd be able to post the complete code (other than the connection string) - and it would be easier to debug and iterate on. – Jon Skeet Mar 15 '18 at 08:12
  • tried as `byte[] ImageData = FileUpload1.FileBytes;` and evverything I know but still throwing exception :'( – Farrukh Sarmad Mar 15 '18 at 08:34
  • 1
    Please read my previous comment carefully. Provide a [mcve] console app to remove the ASP.NET part entirely. Try using BLOB or BFILE instead of LONG RAW, as per APC's ansewr. – Jon Skeet Mar 15 '18 at 08:35
  • You should use bind parameter for **all** values, not just for `party_image`, resp. `blobtodb` – Wernfried Domscheit Mar 15 '18 at 10:41

1 Answers1

1

Why are you using LONG RAW to store binary objects? That's a datatype which has been deprecated for over twenty years.

If you define PARTY_IMAGE as a BLOB (or maybe BFILE) you will find it a lot easier to work with. Find out more.

APC
  • 144,005
  • 19
  • 170
  • 281