uploading image to mysql table

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • John Smith

    uploading image to mysql table

    I know that uploading an image to a database has been covered, oh, about 3
    trillion times. However, I haven't found anything covering uploading to a
    MySQL database with .net. Please don't recommend storing the image to the
    filesystem and only keeping a pointer to that in the table. I want to dump
    the image to a table. My code dumps the data into the table, however, I get
    the following error when trying to view the image "the image ... cannot be
    displayed because it contains errors".

    The image data is stored in a large-blob field, extension is stored in
    varchar, and a description is stored in a varchar. I think the problem
    has to do with special characters in the image data. MySQL seems to be real
    picky about that stuff. Looking at
    http://dev.mysql.com/doc/mysql/en/String_syntax.html, there's a blurb " If
    you want to insert binary data into a string column (such as a BLOB), the
    following characters must be represented by escape sequences: " towards the
    bottom.

    So, I guess I need to somehow parse the inputstream and/or byte array and
    replace those characters as needed. This is where I'm stuck. I could write
    a function to replace the data with the proper escape sequence but each byte
    in the arrFile array contains an integer value as shown with the loop:

    ***********
    for (int i=0; i<=intFileLen-1; i++)
    Response.Write( arrFile[i]);
    ***********

    Is there anyway to view the raw data or is that the raw data? Any ideas?
    Thanks in advance. The code follows


    // Determine File Type
    int fileLen = txtFileContents .PostedFile.Fil eName.Length;
    strFileExtensio n =
    txtFileContents .PostedFile.Fil eName.Substring (fileLen-4,4);

    // Grab the contents of uploaded file
    intFileLen = txtFileContents .PostedFile.Con tentLength;

    byte[] arrFile = new byte[intFileLen];
    objStream = txtFileContents .PostedFile.Inp utStream;
    objStream.Read( arrFile, 0, intFileLen );
    objStream.Close ();

    // Add Uploaded file to database
    strInsert = "Insert Into pics ( TITLE, IMAGE_TYPE, IMAGE ) Values (\""+
    txtFileTitle.Te xt+"\", \""+strFileType +"\", \""+arrFile+ "\" )";

    cmdInsert = new OdbcCommand( strInsert, conMyData );

    conMyData.Open( );
    cmdInsert.Execu teNonQuery();

    conMyData.Close ();
    }


  • Sunny

    #2
    Re: uploading image to mysql table

    Hi,
    I haven't done that, but as I see, you are using the ODBC driver for
    MySQL. Have you tried their ADO.Net provider as well. As I looked thru
    code, it supports binary blobs, so maybe this is the way to go.

    Sunny


    In article <uyfVEUUXEHA.30 44@TK2MSFTNGP09 .phx.gbl>,
    no_one_home@hot mail.com says...[color=blue]
    > I know that uploading an image to a database has been covered, oh, about 3
    > trillion times. However, I haven't found anything covering uploading to a
    > MySQL database with .net. Please don't recommend storing the image to the
    > filesystem and only keeping a pointer to that in the table. I want to dump
    > the image to a table. My code dumps the data into the table, however, I get
    > the following error when trying to view the image "the image ... cannot be
    > displayed because it contains errors".
    >
    > The image data is stored in a large-blob field, extension is stored in
    > varchar, and a description is stored in a varchar. I think the problem
    > has to do with special characters in the image data. MySQL seems to be real
    > picky about that stuff. Looking at
    > http://dev.mysql.com/doc/mysql/en/String_syntax.html, there's a blurb " If
    > you want to insert binary data into a string column (such as a BLOB), the
    > following characters must be represented by escape sequences: " towards the
    > bottom.
    >
    > So, I guess I need to somehow parse the inputstream and/or byte array and
    > replace those characters as needed. This is where I'm stuck. I could write
    > a function to replace the data with the proper escape sequence but each byte
    > in the arrFile array contains an integer value as shown with the loop:
    >
    > ***********
    > for (int i=0; i<=intFileLen-1; i++)
    > Response.Write( arrFile[i]);
    > ***********
    >
    > Is there anyway to view the raw data or is that the raw data? Any ideas?
    > Thanks in advance. The code follows
    >
    >
    > // Determine File Type
    > int fileLen = txtFileContents .PostedFile.Fil eName.Length;
    > strFileExtensio n =
    > txtFileContents .PostedFile.Fil eName.Substring (fileLen-4,4);
    >
    > // Grab the contents of uploaded file
    > intFileLen = txtFileContents .PostedFile.Con tentLength;
    >
    > byte[] arrFile = new byte[intFileLen];
    > objStream = txtFileContents .PostedFile.Inp utStream;
    > objStream.Read( arrFile, 0, intFileLen );
    > objStream.Close ();
    >
    > // Add Uploaded file to database
    > strInsert = "Insert Into pics ( TITLE, IMAGE_TYPE, IMAGE ) Values (\""+
    > txtFileTitle.Te xt+"\", \""+strFileType +"\", \""+arrFile+ "\" )";
    >
    > cmdInsert = new OdbcCommand( strInsert, conMyData );
    >
    > conMyData.Open( );
    > cmdInsert.Execu teNonQuery();
    >
    > conMyData.Close ();
    > }
    >
    >
    >[/color]

    Comment

    • John Smith

      #3
      Re: uploading image to mysql table

      Sunny--

      Thanks for your reply. Are you talking about the ByteFX provider? No I
      hadn't tried that but I just did. Same thing.

      I did manage to figure out how to replace special characters in the data
      with escape sequences...I think. Though, it still doesn't work. What I did
      was convert the image data byte array into an ascii encoded string, used the
      Replace method to replace the special chars with escape sequences and then
      converted that back to a byte array. Like I said, though, it still didn't
      work. Code is below:

      // txtFileContents is the text upload control
      //grab contents and put into byte array
      intFileLen = txtFileContents .PostedFile.Con tentLength;
      byte[] arrFile = new byte[intFileLen];
      objStream = txtFileContents .PostedFile.Inp utStream;
      objStream.Read( arrFile, 0, intFileLen );
      objStream.Close ();

      // now convert that to string and process escape sequences as needed
      Encoding encEncoder = ASCIIEncoding.A SCII;
      string str = encEncoder.GetS tring(arrFile,0 ,arrFile.GetLen gth(0));
      string str2 =
      str.Replace("\" ","\\\"").Repla ce("'","\\'").R eplace("\\","\\ \\").Replace((( c
      har)0).ToString (),"\\0");

      // now convert back to byte array
      byte[] arrCrap = unicode.GetByte s(str2);

      // insert into table...left out the ado connection stuff
      strInsert = "Insert Into pics ( TITLE, IMAGE_TYPE, IMAGE ) Values (\""+
      txtFileTitle.Te xt+"\", \""+strFileType +"\", \""+arrCrap+ "\" )";

      // end
      //////////////////////////////

      "Sunny" <sunnyask@icebe rgwireless.com> wrote in message
      news:O5bx$meXEH A.2940@TK2MSFTN GP09.phx.gbl...[color=blue]
      > Hi,
      > I haven't done that, but as I see, you are using the ODBC driver for
      > MySQL. Have you tried their ADO.Net provider as well. As I looked thru
      > code, it supports binary blobs, so maybe this is the way to go.
      >
      > Sunny
      >
      >
      > In article <uyfVEUUXEHA.30 44@TK2MSFTNGP09 .phx.gbl>,
      > no_one_home@hot mail.com says...[color=green]
      > > I know that uploading an image to a database has been covered, oh, about[/color][/color]
      3[color=blue][color=green]
      > > trillion times. However, I haven't found anything covering uploading to[/color][/color]
      a[color=blue][color=green]
      > > MySQL database with .net. Please don't recommend storing the image to[/color][/color]
      the[color=blue][color=green]
      > > filesystem and only keeping a pointer to that in the table. I want to[/color][/color]
      dump[color=blue][color=green]
      > > the image to a table. My code dumps the data into the table, however, I[/color][/color]
      get[color=blue][color=green]
      > > the following error when trying to view the image "the image ... cannot[/color][/color]
      be[color=blue][color=green]
      > > displayed because it contains errors".
      > >
      > > The image data is stored in a large-blob field, extension is stored in
      > > varchar, and a description is stored in a varchar. I think the[/color][/color]
      problem[color=blue][color=green]
      > > has to do with special characters in the image data. MySQL seems to be[/color][/color]
      real[color=blue][color=green]
      > > picky about that stuff. Looking at
      > > http://dev.mysql.com/doc/mysql/en/String_syntax.html, there's a blurb "[/color][/color]
      If[color=blue][color=green]
      > > you want to insert binary data into a string column (such as a BLOB),[/color][/color]
      the[color=blue][color=green]
      > > following characters must be represented by escape sequences: " towards[/color][/color]
      the[color=blue][color=green]
      > > bottom.
      > >
      > > So, I guess I need to somehow parse the inputstream and/or byte array[/color][/color]
      and[color=blue][color=green]
      > > replace those characters as needed. This is where I'm stuck. I could[/color][/color]
      write[color=blue][color=green]
      > > a function to replace the data with the proper escape sequence but each[/color][/color]
      byte[color=blue][color=green]
      > > in the arrFile array contains an integer value as shown with the loop:
      > >
      > > ***********
      > > for (int i=0; i<=intFileLen-1; i++)
      > > Response.Write( arrFile[i]);
      > > ***********
      > >
      > > Is there anyway to view the raw data or is that the raw data? Any[/color][/color]
      ideas?[color=blue][color=green]
      > > Thanks in advance. The code follows
      > >
      > >
      > > // Determine File Type
      > > int fileLen = txtFileContents .PostedFile.Fil eName.Length;
      > > strFileExtensio n =
      > > txtFileContents .PostedFile.Fil eName.Substring (fileLen-4,4);
      > >
      > > // Grab the contents of uploaded file
      > > intFileLen = txtFileContents .PostedFile.Con tentLength;
      > >
      > > byte[] arrFile = new byte[intFileLen];
      > > objStream = txtFileContents .PostedFile.Inp utStream;
      > > objStream.Read( arrFile, 0, intFileLen );
      > > objStream.Close ();
      > >
      > > // Add Uploaded file to database
      > > strInsert = "Insert Into pics ( TITLE, IMAGE_TYPE, IMAGE ) Values[/color][/color]
      (\""+[color=blue][color=green]
      > > txtFileTitle.Te xt+"\", \""+strFileType +"\", \""+arrFile+ "\" )";
      > >
      > > cmdInsert = new OdbcCommand( strInsert, conMyData );
      > >
      > > conMyData.Open( );
      > > cmdInsert.Execu teNonQuery();
      > >
      > > conMyData.Close ();
      > > }
      > >
      > >
      > >[/color][/color]


      Comment

      • Sunny

        #4
        Re: uploading image to mysql table

        Hi John,



        In article <uCgiSvgXEHA.36 68@TK2MSFTNGP09 .phx.gbl>,
        no_one_home@hot mail.com says...[color=blue]
        > Sunny--
        >
        > Thanks for your reply. Are you talking about the ByteFX provider? No I
        > hadn't tried that but I just did. Same thing.[/color]

        Yes, that was my idea.
        [color=blue]
        > // now convert back to byte array
        > byte[] arrCrap = unicode.GetByte s(str2);
        >
        > // insert into table...left out the ado connection stuff
        > strInsert = "Insert Into pics ( TITLE, IMAGE_TYPE, IMAGE ) Values (\""+
        > txtFileTitle.Te xt+"\", \""+strFileType +"\", \""+arrCrap+ "\" )";
        >[/color]


        And how this works??? How can you add byte[] a part of string
        concatenation? Does the last line compile at all?

        Sunny

        Comment

        • John Smith

          #5
          Re: uploading image to mysql table

          Sunny--

          Yup. The program compiles and runs fine. I don't know how or why it works
          but this is the same code I got from my ASP.Net Unleashed book. Of course,
          they are using a MSSQL server for the dbms and it has an "image" data type
          built in. MySQL has only the BLOB type.



          "Sunny" <sunnyask@icebe rgwireless.com> wrote in message
          news:%23RWSdfrX EHA.3668@TK2MSF TNGP09.phx.gbl. ..[color=blue]
          > Hi John,
          >
          >
          >
          > In article <uCgiSvgXEHA.36 68@TK2MSFTNGP09 .phx.gbl>,
          > no_one_home@hot mail.com says...[color=green]
          > > Sunny--
          > >
          > > Thanks for your reply. Are you talking about the ByteFX provider? No I
          > > hadn't tried that but I just did. Same thing.[/color]
          >
          > Yes, that was my idea.
          >[color=green]
          > > // now convert back to byte array
          > > byte[] arrCrap = unicode.GetByte s(str2);
          > >
          > > // insert into table...left out the ado connection stuff
          > > strInsert = "Insert Into pics ( TITLE, IMAGE_TYPE, IMAGE ) Values (\""+
          > > txtFileTitle.Te xt+"\", \""+strFileType +"\", \""+arrCrap+ "\" )";
          > >[/color]
          >
          >
          > And how this works??? How can you add byte[] a part of string
          > concatenation? Does the last line compile at all?
          >
          > Sunny[/color]


          Comment

          • Sunny

            #6
            Re: uploading image to mysql table

            Hi John,

            this code is broken. After executing this line the content of the
            strInsert is:

            Insert Into pics ( TITLE, IMAGE_TYPE, IMAGE ) Values ("myfie.txt" ,
            "myImageTyp e", "System.Byt e[]" )

            As you can see, putting a byte[] in the string concatenation actually
            invokes the .ToString method of the Array class, which in this case
            outputs only "System.Byt e[]", but not the actual content of the array.

            So, try to replace this:
            strInsert = "Insert Into pics ( TITLE, IMAGE_TYPE, IMAGE ) Values (\""+
            txtFileTitle.Te xt+"\", \""+strFileType +"\", \""+arrCrap+ "\" )";

            with this:
            strInsert = "Insert Into pics ( TITLE, IMAGE_TYPE, IMAGE ) Values (\""+
            txtFileTitle.Te xt+"\", \""+strFileType +"\", \""+str2+"\" )";

            Now your modified string will be included in the insert statement.

            I can not test it now, but at least removes an obvious problem.


            Sunny


            In article <eknwzQtXEHA.95 2@TK2MSFTNGP10. phx.gbl>,
            no_one_home@hot mail.com says...[color=blue]
            > Sunny--
            >
            > Yup. The program compiles and runs fine. I don't know how or why it works
            > but this is the same code I got from my ASP.Net Unleashed book. Of course,
            > they are using a MSSQL server for the dbms and it has an "image" data type
            > built in. MySQL has only the BLOB type.
            >
            >
            >
            > "Sunny" <sunnyask@icebe rgwireless.com> wrote in message
            > news:%23RWSdfrX EHA.3668@TK2MSF TNGP09.phx.gbl. ..[color=green]
            > > Hi John,
            > >
            > >
            > >
            > > In article <uCgiSvgXEHA.36 68@TK2MSFTNGP09 .phx.gbl>,
            > > no_one_home@hot mail.com says...[color=darkred]
            > > > Sunny--
            > > >
            > > > Thanks for your reply. Are you talking about the ByteFX provider? No I
            > > > hadn't tried that but I just did. Same thing.[/color]
            > >
            > > Yes, that was my idea.
            > >[color=darkred]
            > > > // now convert back to byte array
            > > > byte[] arrCrap = unicode.GetByte s(str2);
            > > >
            > > > // insert into table...left out the ado connection stuff
            > > > strInsert = "Insert Into pics ( TITLE, IMAGE_TYPE, IMAGE ) Values (\""+
            > > > txtFileTitle.Te xt+"\", \""+strFileType +"\", \""+arrCrap+ "\" )";
            > > >[/color]
            > >
            > >
            > > And how this works??? How can you add byte[] a part of string
            > > concatenation? Does the last line compile at all?
            > >
            > > Sunny[/color]
            >
            >
            >[/color]

            Comment

            • John Smith

              #7
              Re: uploading image to mysql table

              Sunny--

              I had wondered about that. The data that's actually in the table does indeed
              show as "System.Byt e[]". But when I double-clicked that in the MySQL
              control center, it loaded the image viewer app. That led me to think there
              was something else there. The book I'm using has this:

              /////////////////////////////////////
              // Add Uploaded file to database
              conMyData = new SqlConnection( @"Server=localh ost;Integrated
              Security=SSPI;D atabase=myData" );
              strInsert = "Insert Uploads ( u_title, u_documentType, u_document ) " +
              "Values (@title, @fileType, @document )";
              cmdInsert = new SqlCommand( strInsert, conMyData );
              cmdInsert.Param eters.Add( "@title", txtFileTitle.Te xt );
              cmdInsert.Param eters.Add( "@fileType" , strFileType );
              cmdInsert.Param eters.Add( "@document" , arrFile );
              conMyData.Open( );
              cmdInsert.Execu teNonQuery();
              conMyData.Close ();
              /////////////////////////////////////

              But using the Parameters.Add failed when I tried so I ended up with what you
              see. Is the code above not doing the exact same thing? Regardless I did
              try the using the str2 variable. It didn't work either. I'm assuming
              because it's a string and not binary data? Can you tell I don't know what
              I'm doing? :)

              Thanks for your willingness to help me on this.






              "Sunny" <sunnyask@icebe rgwireless.com> wrote in message
              news:Om0b5wtXEH A.2664@TK2MSFTN GP09.phx.gbl...[color=blue]
              > Hi John,
              >
              > this code is broken. After executing this line the content of the
              > strInsert is:
              >
              > Insert Into pics ( TITLE, IMAGE_TYPE, IMAGE ) Values ("myfie.txt" ,
              > "myImageTyp e", "System.Byt e[]" )
              >
              > As you can see, putting a byte[] in the string concatenation actually
              > invokes the .ToString method of the Array class, which in this case
              > outputs only "System.Byt e[]", but not the actual content of the array.
              >
              > So, try to replace this:
              > strInsert = "Insert Into pics ( TITLE, IMAGE_TYPE, IMAGE ) Values (\""+
              > txtFileTitle.Te xt+"\", \""+strFileType +"\", \""+arrCrap+ "\" )";
              >
              > with this:
              > strInsert = "Insert Into pics ( TITLE, IMAGE_TYPE, IMAGE ) Values (\""+
              > txtFileTitle.Te xt+"\", \""+strFileType +"\", \""+str2+"\" )";
              >
              > Now your modified string will be included in the insert statement.
              >
              > I can not test it now, but at least removes an obvious problem.
              >
              >
              > Sunny
              >
              >
              > In article <eknwzQtXEHA.95 2@TK2MSFTNGP10. phx.gbl>,
              > no_one_home@hot mail.com says...[color=green]
              > > Sunny--
              > >
              > > Yup. The program compiles and runs fine. I don't know how or why it[/color][/color]
              works[color=blue][color=green]
              > > but this is the same code I got from my ASP.Net Unleashed book. Of[/color][/color]
              course,[color=blue][color=green]
              > > they are using a MSSQL server for the dbms and it has an "image" data[/color][/color]
              type[color=blue][color=green]
              > > built in. MySQL has only the BLOB type.
              > >
              > >
              > >
              > > "Sunny" <sunnyask@icebe rgwireless.com> wrote in message
              > > news:%23RWSdfrX EHA.3668@TK2MSF TNGP09.phx.gbl. ..[color=darkred]
              > > > Hi John,
              > > >
              > > >
              > > >
              > > > In article <uCgiSvgXEHA.36 68@TK2MSFTNGP09 .phx.gbl>,
              > > > no_one_home@hot mail.com says...
              > > > > Sunny--
              > > > >
              > > > > Thanks for your reply. Are you talking about the ByteFX provider?[/color][/color][/color]
              No I[color=blue][color=green][color=darkred]
              > > > > hadn't tried that but I just did. Same thing.
              > > >
              > > > Yes, that was my idea.
              > > >
              > > > > // now convert back to byte array
              > > > > byte[] arrCrap = unicode.GetByte s(str2);
              > > > >
              > > > > // insert into table...left out the ado connection stuff
              > > > > strInsert = "Insert Into pics ( TITLE, IMAGE_TYPE, IMAGE ) Values[/color][/color][/color]
              (\""+[color=blue][color=green][color=darkred]
              > > > > txtFileTitle.Te xt+"\", \""+strFileType +"\", \""+arrCrap+ "\" )";
              > > > >
              > > >
              > > >
              > > > And how this works??? How can you add byte[] a part of string
              > > > concatenation? Does the last line compile at all?
              > > >
              > > > Sunny[/color]
              > >
              > >
              > >[/color][/color]


              Comment

              • Sunny

                #8
                Re: uploading image to mysql table

                Now I see the first mistake :)
                Parameters.Add does a lot more work than just replacing strings in the
                command text. Now, lets try to solve the problem. I have no time now to
                install MySql and test, so, please, check this:

                1. After you read the file into the array, can you create an image from
                this array? Without any manipulations?

                2. If step one is OK: using ByteFX lib, recreate the last part of code
                you have posted:

                conMyData = new MySqlConnection ....

                cmdInsert = new MySqlCommand... ..

                all the Add commands, etc.

                If something fails, please report the exact exception you receive.

                3. If step 2 is OK, and you can see some data in the DB, but still can
                not view it, please read it back using ByteFX library and compare the
                received data to originally stored data. You may try to create an image,
                or you can write it to a file and compare both files to see what is the
                difference.

                Sunny

                In article <OavqdXuXEHA.64 4@tk2msftngp13. phx.gbl>,
                no_one_home@hot mail.com says...[color=blue]
                > Sunny--
                >
                > I had wondered about that. The data that's actually in the table does indeed
                > show as "System.Byt e[]". But when I double-clicked that in the MySQL
                > control center, it loaded the image viewer app. That led me to think there
                > was something else there. The book I'm using has this:
                >
                > /////////////////////////////////////
                > // Add Uploaded file to database
                > conMyData = new SqlConnection( @"Server=localh ost;Integrated
                > Security=SSPI;D atabase=myData" );
                > strInsert = "Insert Uploads ( u_title, u_documentType, u_document ) " +
                > "Values (@title, @fileType, @document )";
                > cmdInsert = new SqlCommand( strInsert, conMyData );
                > cmdInsert.Param eters.Add( "@title", txtFileTitle.Te xt );
                > cmdInsert.Param eters.Add( "@fileType" , strFileType );
                > cmdInsert.Param eters.Add( "@document" , arrFile );
                > conMyData.Open( );
                > cmdInsert.Execu teNonQuery();
                > conMyData.Close ();
                > /////////////////////////////////////
                >
                > But using the Parameters.Add failed when I tried so I ended up with what you
                > see. Is the code above not doing the exact same thing? Regardless I did
                > try the using the str2 variable. It didn't work either. I'm assuming
                > because it's a string and not binary data? Can you tell I don't know what
                > I'm doing? :)
                >
                > Thanks for your willingness to help me on this.
                >
                >
                >
                >
                >
                >
                > "Sunny" <sunnyask@icebe rgwireless.com> wrote in message
                > news:Om0b5wtXEH A.2664@TK2MSFTN GP09.phx.gbl...[color=green]
                > > Hi John,
                > >
                > > this code is broken. After executing this line the content of the
                > > strInsert is:
                > >
                > > Insert Into pics ( TITLE, IMAGE_TYPE, IMAGE ) Values ("myfie.txt" ,
                > > "myImageTyp e", "System.Byt e[]" )
                > >
                > > As you can see, putting a byte[] in the string concatenation actually
                > > invokes the .ToString method of the Array class, which in this case
                > > outputs only "System.Byt e[]", but not the actual content of the array.
                > >
                > > So, try to replace this:
                > > strInsert = "Insert Into pics ( TITLE, IMAGE_TYPE, IMAGE ) Values (\""+
                > > txtFileTitle.Te xt+"\", \""+strFileType +"\", \""+arrCrap+ "\" )";
                > >
                > > with this:
                > > strInsert = "Insert Into pics ( TITLE, IMAGE_TYPE, IMAGE ) Values (\""+
                > > txtFileTitle.Te xt+"\", \""+strFileType +"\", \""+str2+"\" )";
                > >
                > > Now your modified string will be included in the insert statement.
                > >
                > > I can not test it now, but at least removes an obvious problem.
                > >
                > >
                > > Sunny
                > >
                > >
                > > In article <eknwzQtXEHA.95 2@TK2MSFTNGP10. phx.gbl>,
                > > no_one_home@hot mail.com says...[color=darkred]
                > > > Sunny--
                > > >
                > > > Yup. The program compiles and runs fine. I don't know how or why it[/color][/color]
                > works[color=green][color=darkred]
                > > > but this is the same code I got from my ASP.Net Unleashed book. Of[/color][/color]
                > course,[color=green][color=darkred]
                > > > they are using a MSSQL server for the dbms and it has an "image" data[/color][/color]
                > type[color=green][color=darkred]
                > > > built in. MySQL has only the BLOB type.
                > > >
                > > >
                > > >
                > > > "Sunny" <sunnyask@icebe rgwireless.com> wrote in message
                > > > news:%23RWSdfrX EHA.3668@TK2MSF TNGP09.phx.gbl. ..
                > > > > Hi John,
                > > > >
                > > > >
                > > > >
                > > > > In article <uCgiSvgXEHA.36 68@TK2MSFTNGP09 .phx.gbl>,
                > > > > no_one_home@hot mail.com says...
                > > > > > Sunny--
                > > > > >
                > > > > > Thanks for your reply. Are you talking about the ByteFX provider?[/color][/color]
                > No I[color=green][color=darkred]
                > > > > > hadn't tried that but I just did. Same thing.
                > > > >
                > > > > Yes, that was my idea.
                > > > >
                > > > > > // now convert back to byte array
                > > > > > byte[] arrCrap = unicode.GetByte s(str2);
                > > > > >
                > > > > > // insert into table...left out the ado connection stuff
                > > > > > strInsert = "Insert Into pics ( TITLE, IMAGE_TYPE, IMAGE ) Values[/color][/color]
                > (\""+[color=green][color=darkred]
                > > > > > txtFileTitle.Te xt+"\", \""+strFileType +"\", \""+arrCrap+ "\" )";
                > > > > >
                > > > >
                > > > >
                > > > > And how this works??? How can you add byte[] a part of string
                > > > > concatenation? Does the last line compile at all?
                > > > >
                > > > > Sunny
                > > >
                > > >
                > > >[/color][/color]
                >
                >
                >[/color]

                Comment

                • John Smith

                  #9
                  Re: uploading image to mysql table

                  Success!!!! Many, many thanks Sunny. That was the problem.

                  Final code for anyone interested is:
                  // upload.aspx
                  //////////////////////////////////////////////////////////////
                  void Button_Click(Ob ject sender , EventArgs e)
                  {
                  string strFileExtensio n;
                  string strFileType;
                  int intFileLen;
                  Stream objStream;
                  string strInsert;

                  if ( txtFileContents .PostedFile != null )
                  {
                  // Determine File Type
                  int fileLen = txtFileContents .PostedFile.Fil eName.Length;
                  strFileExtensio n =
                  txtFileContents .PostedFile.Fil eName.Substring (fileLen-4,4);
                  switch (strFileExtensi on.ToLower()) {
                  case ".bmp":
                  strFileType = "bmp";
                  break;
                  case ".jpg":
                  strFileType = "jpg";
                  break;
                  case ".gif":
                  strFileType = "gif";
                  break;
                  }

                  // Grab the contents of uploaded file
                  intFileLen = txtFileContents .PostedFile.Con tentLength;
                  byte[] arrFile = new byte[intFileLen];
                  objStream = txtFileContents .PostedFile.Inp utStream;
                  objStream.Read( arrFile, 0, intFileLen );
                  objStream.Close ();

                  // Add Uploaded file to database
                  string myConnectionStr ing = "Database=a_mys ql_db;Data
                  Source=localhos t;User Id=userid;Passw ord=passwd";

                  MySqlConnection myConnection = new MySqlConnection (myConnectionSt ring);

                  string myInsertQuery = "Insert Into pics (TITLE,IMAGE_TY PE,IMAGE ) Values
                  (@title,@imaget ype,@image)";
                  MySqlCommand myCommand = new MySqlCommand(my InsertQuery);
                  myCommand.Param eters.Add("@tit le","blah");
                  myCommand.Param eters.Add("@ima getype","jpg");
                  myCommand.Param eters.Add("@ima ge",arrFile);

                  myCommand.Conne ction = myConnection;
                  myConnection.Op en();
                  myCommand.Execu teNonQuery();
                  myCommand.Conne ction.Close();

                  }

                  }
                  //////////////////////////////////////////////////////////////
                  // viewimage.aspx
                  void Page_Load(Objec t sender , EventArgs e)
                  {
                  int intItemID;
                  OdbcConnection conMyData;
                  string strSelect;
                  OdbcCommand cmdSelect;
                  OdbcDataReader dtrSearch;

                  intItemID = Convert.ToInt32 (Request.Params["id"]);

                  conMyData = new OdbcConnection( "DSN=a_dsn" ); // this hasn't been switched
                  to the ado.net provider

                  strSelect = "SELECT IMAGE_TYPE, IMAGE, TITLE From pics "
                  + "WHERE PIC_ID=35"; // hard coded record...will really pull from
                  querystring

                  cmdSelect = new OdbcCommand( strSelect, conMyData );
                  conMyData.Open( );

                  dtrSearch = cmdSelect.Execu teReader();

                  if ( dtrSearch.Read( ))
                  {
                  // set Content Type
                  Response.ClearH eaders();
                  Response.Append Header("Content-Transfer-Encoding","bina ry");

                  Response.Conten tType = "image/jpeg";
                  Response.Binary Write( (byte[])dtrSearch["IMAGE"] );
                  }

                  dtrSearch.Close ();
                  conMyData.Close ();
                  Response.End();
                  }

                  // now you can call viewimage.aspx within an <img> tag like so:
                  <img src="viewimage. aspx?image_id=x " />

                  ///////////////////////////////////////////////////////////////






                  "Sunny" <sunnyask@icebe rgwireless.com> wrote in message
                  news:uJRhnzuXEH A.2868@TK2MSFTN GP09.phx.gbl...[color=blue]
                  > Now I see the first mistake :)
                  > Parameters.Add does a lot more work than just replacing strings in the
                  > command text. Now, lets try to solve the problem. I have no time now to
                  > install MySql and test, so, please, check this:
                  >
                  > 1. After you read the file into the array, can you create an image from
                  > this array? Without any manipulations?
                  >
                  > 2. If step one is OK: using ByteFX lib, recreate the last part of code
                  > you have posted:
                  >
                  > conMyData = new MySqlConnection ....
                  >
                  > cmdInsert = new MySqlCommand... ..
                  >
                  > all the Add commands, etc.
                  >
                  > If something fails, please report the exact exception you receive.
                  >
                  > 3. If step 2 is OK, and you can see some data in the DB, but still can
                  > not view it, please read it back using ByteFX library and compare the
                  > received data to originally stored data. You may try to create an image,
                  > or you can write it to a file and compare both files to see what is the
                  > difference.
                  >
                  > Sunny
                  >
                  > In article <OavqdXuXEHA.64 4@tk2msftngp13. phx.gbl>,
                  > no_one_home@hot mail.com says...[color=green]
                  > > Sunny--
                  > >
                  > > I had wondered about that. The data that's actually in the table does[/color][/color]
                  indeed[color=blue][color=green]
                  > > show as "System.Byt e[]". But when I double-clicked that in the MySQL
                  > > control center, it loaded the image viewer app. That led me to think[/color][/color]
                  there[color=blue][color=green]
                  > > was something else there. The book I'm using has this:
                  > >
                  > > /////////////////////////////////////
                  > > // Add Uploaded file to database
                  > > conMyData = new SqlConnection( @"Server=localh ost;Integrated
                  > > Security=SSPI;D atabase=myData" );
                  > > strInsert = "Insert Uploads ( u_title, u_documentType, u_document ) "[/color][/color]
                  +[color=blue][color=green]
                  > > "Values (@title, @fileType, @document )";
                  > > cmdInsert = new SqlCommand( strInsert, conMyData );
                  > > cmdInsert.Param eters.Add( "@title", txtFileTitle.Te xt );
                  > > cmdInsert.Param eters.Add( "@fileType" , strFileType );
                  > > cmdInsert.Param eters.Add( "@document" , arrFile );
                  > > conMyData.Open( );
                  > > cmdInsert.Execu teNonQuery();
                  > > conMyData.Close ();
                  > > /////////////////////////////////////
                  > >
                  > > But using the Parameters.Add failed when I tried so I ended up with what[/color][/color]
                  you[color=blue][color=green]
                  > > see. Is the code above not doing the exact same thing? Regardless I[/color][/color]
                  did[color=blue][color=green]
                  > > try the using the str2 variable. It didn't work either. I'm assuming
                  > > because it's a string and not binary data? Can you tell I don't know[/color][/color]
                  what[color=blue][color=green]
                  > > I'm doing? :)
                  > >
                  > > Thanks for your willingness to help me on this.
                  > >
                  > >
                  > >
                  > >
                  > >
                  > >
                  > > "Sunny" <sunnyask@icebe rgwireless.com> wrote in message
                  > > news:Om0b5wtXEH A.2664@TK2MSFTN GP09.phx.gbl...[color=darkred]
                  > > > Hi John,
                  > > >
                  > > > this code is broken. After executing this line the content of the
                  > > > strInsert is:
                  > > >
                  > > > Insert Into pics ( TITLE, IMAGE_TYPE, IMAGE ) Values ("myfie.txt" ,
                  > > > "myImageTyp e", "System.Byt e[]" )
                  > > >
                  > > > As you can see, putting a byte[] in the string concatenation actually
                  > > > invokes the .ToString method of the Array class, which in this case
                  > > > outputs only "System.Byt e[]", but not the actual content of the array.
                  > > >
                  > > > So, try to replace this:
                  > > > strInsert = "Insert Into pics ( TITLE, IMAGE_TYPE, IMAGE ) Values[/color][/color][/color]
                  (\""+[color=blue][color=green][color=darkred]
                  > > > txtFileTitle.Te xt+"\", \""+strFileType +"\", \""+arrCrap+ "\" )";
                  > > >
                  > > > with this:
                  > > > strInsert = "Insert Into pics ( TITLE, IMAGE_TYPE, IMAGE ) Values[/color][/color][/color]
                  (\""+[color=blue][color=green][color=darkred]
                  > > > txtFileTitle.Te xt+"\", \""+strFileType +"\", \""+str2+"\" )";
                  > > >
                  > > > Now your modified string will be included in the insert statement.
                  > > >
                  > > > I can not test it now, but at least removes an obvious problem.
                  > > >
                  > > >
                  > > > Sunny
                  > > >
                  > > >
                  > > > In article <eknwzQtXEHA.95 2@TK2MSFTNGP10. phx.gbl>,
                  > > > no_one_home@hot mail.com says...
                  > > > > Sunny--
                  > > > >
                  > > > > Yup. The program compiles and runs fine. I don't know how or why it[/color]
                  > > works[color=darkred]
                  > > > > but this is the same code I got from my ASP.Net Unleashed book. Of[/color]
                  > > course,[color=darkred]
                  > > > > they are using a MSSQL server for the dbms and it has an "image"[/color][/color][/color]
                  data[color=blue][color=green]
                  > > type[color=darkred]
                  > > > > built in. MySQL has only the BLOB type.
                  > > > >
                  > > > >
                  > > > >
                  > > > > "Sunny" <sunnyask@icebe rgwireless.com> wrote in message
                  > > > > news:%23RWSdfrX EHA.3668@TK2MSF TNGP09.phx.gbl. ..
                  > > > > > Hi John,
                  > > > > >
                  > > > > >
                  > > > > >
                  > > > > > In article <uCgiSvgXEHA.36 68@TK2MSFTNGP09 .phx.gbl>,
                  > > > > > no_one_home@hot mail.com says...
                  > > > > > > Sunny--
                  > > > > > >
                  > > > > > > Thanks for your reply. Are you talking about the ByteFX[/color][/color][/color]
                  provider?[color=blue][color=green]
                  > > No I[color=darkred]
                  > > > > > > hadn't tried that but I just did. Same thing.
                  > > > > >
                  > > > > > Yes, that was my idea.
                  > > > > >
                  > > > > > > // now convert back to byte array
                  > > > > > > byte[] arrCrap = unicode.GetByte s(str2);
                  > > > > > >
                  > > > > > > // insert into table...left out the ado connection stuff
                  > > > > > > strInsert = "Insert Into pics ( TITLE, IMAGE_TYPE, IMAGE )[/color][/color][/color]
                  Values[color=blue][color=green]
                  > > (\""+[color=darkred]
                  > > > > > > txtFileTitle.Te xt+"\", \""+strFileType +"\",[/color][/color][/color]
                  \""+arrCrap+ "\" )";[color=blue][color=green][color=darkred]
                  > > > > > >
                  > > > > >
                  > > > > >
                  > > > > > And how this works??? How can you add byte[] a part of string
                  > > > > > concatenation? Does the last line compile at all?
                  > > > > >
                  > > > > > Sunny
                  > > > >
                  > > > >
                  > > > >[/color]
                  > >
                  > >
                  > >[/color][/color]


                  Comment

                  • Sunny

                    #10
                    Re: uploading image to mysql table

                    Hi John,

                    this is good that it works.

                    Now, final words:

                    Always put your data access code in try/finaly block. Always.

                    Your last lines in the oNClick method shoud be:

                    try
                    {
                    myConnection.Op en();
                    myCommand.Execu teNonQuery();
                    }
                    catch //this is optional, if you want to
                    //report the error
                    {
                    // report the error
                    }
                    finally
                    {
                    myConnection.Cl ose();
                    }

                    This way you quarantee that even if something goes wrong, the connection
                    will always be closed.


                    Good luck
                    Sunny

                    Comment

                    • John Smith

                      #11
                      Re: uploading image to mysql table

                      Cool.

                      Thanks again for your help!

                      "Sunny" <sunnyask@icebe rgwireless.com> wrote in message
                      news:u1w8Jy3XEH A.1764@TK2MSFTN GP10.phx.gbl...[color=blue]
                      > Hi John,
                      >
                      > this is good that it works.
                      >
                      > Now, final words:
                      >
                      > Always put your data access code in try/finaly block. Always.
                      >
                      > Your last lines in the oNClick method shoud be:
                      >
                      > try
                      > {
                      > myConnection.Op en();
                      > myCommand.Execu teNonQuery();
                      > }
                      > catch //this is optional, if you want to
                      > //report the error
                      > {
                      > // report the error
                      > }
                      > finally
                      > {
                      > myConnection.Cl ose();
                      > }
                      >
                      > This way you quarantee that even if something goes wrong, the connection
                      > will always be closed.
                      >
                      >
                      > Good luck
                      > Sunny[/color]


                      Comment

                      Working...