Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. Problem saving file to MySQL

Problem saving file to MySQL

Scheduled Pinned Locked Moved Database
helpsharepointdatabasemysqldata-structures
20 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Jassim Rahma

    so i should not use blob nor LongBlob?

    L Offline
    L Offline
    Lost User
    wrote on last edited by
    #4

    Should work too, depending on size, but a VARCHAR won't work. --edit; Typo, forgotten an "o".

    Bastard Programmer from Hell :suss: if you can't read my code, try converting it here[^]

    J 1 Reply Last reply
    0
    • L Lost User

      Should work too, depending on size, but a VARCHAR won't work. --edit; Typo, forgotten an "o".

      Bastard Programmer from Hell :suss: if you can't read my code, try converting it here[^]

      J Offline
      J Offline
      Jassim Rahma
      wrote on last edited by
      #5

      I tried it with blob, longblob and binary but all giving same problem

      L 1 Reply Last reply
      0
      • J Jassim Rahma

        I tried it with blob, longblob and binary but all giving same problem

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #6

        Code & exact error-message - you know the drill :) --edit; I assume that the db-parameter has been set to a binary type, and is no longer the varchar from the first post?

        Bastard Programmer from Hell :suss: if you can't read my code, try converting it here[^]

        J 1 Reply Last reply
        0
        • L Lost User

          Code & exact error-message - you know the drill :) --edit; I assume that the db-parameter has been set to a binary type, and is no longer the varchar from the first post?

          Bastard Programmer from Hell :suss: if you can't read my code, try converting it here[^]

          J Offline
          J Offline
          Jassim Rahma
          wrote on last edited by
          #7

          code and exact error mentioned in my first post.. Thanks in advance

          J 1 Reply Last reply
          0
          • J Jassim Rahma

            code and exact error mentioned in my first post.. Thanks in advance

            J Offline
            J Offline
            jschell
            wrote on last edited by
            #8

            jrahma wrote:

            code and exact error mentioned in my first post..

            Then as you were already told the type that you are using IN THAT CODE, is wrong because it is a varchar. A varchar is not binary.

            J 1 Reply Last reply
            0
            • J jschell

              jrahma wrote:

              code and exact error mentioned in my first post..

              Then as you were already told the type that you are using IN THAT CODE, is wrong because it is a varchar. A varchar is not binary.

              J Offline
              J Offline
              Jassim Rahma
              wrote on last edited by
              #9

              the VARCHAR was for the document format which is text representing the file extension. The document itself is the param_resume_data...

              J 1 Reply Last reply
              0
              • J Jassim Rahma

                I am using this code to save file to MySQL but when I try to open the file after saving it I get an error saying: Word was unable to read this document. It may be corrupt. here is the code to write:

                file_name = Path.GetFileName(uploadResume.PostedFile.FileName);
                file_extension = Path.GetExtension(uploadResume.PostedFile.FileName);

                switch (file_extension)
                {
                case ".pdf": document_type = "application/pdf"; break;
                case ".doc": document_type = "application/vnd.ms-word"; break;
                case ".docx": document_type = "application/vnd.ms-word"; break;
                case ".gif": document_type = "image/gif"; break;
                case ".png": document_type = "image/png"; break;
                case ".jpg": document_type = "image/jpg"; break;
                case ".jpeg": document_type = "image/jpg"; break;
                }

                // calculate size of file;
                int file_size = uploadResume.PostedFile.ContentLength;

                // create array to read the file into it;
                byte[] document_binary = new byte[file_size];
                uploadResume.PostedFile.InputStream.Read(document_binary, 0, file_size);

                and then passing it as parameters:

                sql_command.Parameters.AddWithValue("param_resume_format", document_type).MySqlDbType = MySqlDbType.VarChar;
                sql_command.Parameters.Add("param_resume_data", MySqlDbType.Blob, file_size).Value = document_binary;

                and here is how I am retrieving it:

                sql_connection = new MySqlConnection(ConfigurationManager.ConnectionStrings["SQLdb"].ConnectionString);
                sql_connection.Open();
                sql_command = new MySqlCommand("sp_get_resume_by_id", sql_connection);
                sql_command.CommandType = CommandType.StoredProcedure;
                sql_command.Parameters.Add("param_resume_id", MySqlDbType.Int32).Value = resume_id;
                sql_reader = sql_command.ExecuteReader();
                sql_reader.Read();

                if (sql_reader.HasRows)
                {
                file_name = sql_reader["resume_id"].ToString() + sql_reader["resume_ext"].ToString();
                byte[] document_binary = (byte[])sql_reader["resume_data"];

                FileStream file_stream = new FileStream(@"C:\Temp\" + file_name, FileMode.Create);
                file_stream.Write(document_binary, 0, document_binary.Length);
                file_stream.Close();
                file_stream.Dispose();

                txtResume.Visible = true;
                }

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #10

                ..and this happens for "all" files, without exception? Tried a very small picture? If that's possible, consider the below modification;

                file_stream.Write(document_binary, 0, document_binary.LongLength);

                Bastard Programmer from Hell :suss: if you can't read my code, try converting it here[^]

                J 1 Reply Last reply
                0
                • J Jassim Rahma

                  the VARCHAR was for the document format which is text representing the file extension. The document itself is the param_resume_data...

                  J Offline
                  J Offline
                  jschell
                  wrote on last edited by
                  #11

                  Stating it again.... The type must be binary. The code must reflect that. The code in your first post does NOT reflect that. The request was made that you post your code that has been UPDATED to use binary.

                  1 Reply Last reply
                  0
                  • L Lost User

                    ..and this happens for "all" files, without exception? Tried a very small picture? If that's possible, consider the below modification;

                    file_stream.Write(document_binary, 0, document_binary.LongLength);

                    Bastard Programmer from Hell :suss: if you can't read my code, try converting it here[^]

                    J Offline
                    J Offline
                    Jassim Rahma
                    wrote on last edited by
                    #12

                    same problem with small pic. it's all coming to 1 byte file. i tried changing it to your code like this:

                    file_stream.Write(document_binary, 0, document_binary.LongLength);

                    but getting this error:

                    The best overloaded method match for System.IO.Stream.Write(byte[], int, int) has some invalid arguments

                    Argument 3: cannot convert from 'long' to 'int'

                    L 1 Reply Last reply
                    0
                    • J Jassim Rahma

                      same problem with small pic. it's all coming to 1 byte file. i tried changing it to your code like this:

                      file_stream.Write(document_binary, 0, document_binary.LongLength);

                      but getting this error:

                      The best overloaded method match for System.IO.Stream.Write(byte[], int, int) has some invalid arguments

                      Argument 3: cannot convert from 'long' to 'int'

                      L Offline
                      L Offline
                      Lost User
                      wrote on last edited by
                      #13

                      jrahma wrote:

                      i tried changing it to your code like this:

                      LongLength could be quite big; I suggest you write it in "chuncks" of int.MaxValue;

                      jrahma wrote:

                      it's all coming to 1 byte file.

                      I hope that there's more than one byte in the database?

                      Bastard Programmer from Hell :suss: if you can't read my code, try converting it here[^]

                      J 1 Reply Last reply
                      0
                      • L Lost User

                        jrahma wrote:

                        i tried changing it to your code like this:

                        LongLength could be quite big; I suggest you write it in "chuncks" of int.MaxValue;

                        jrahma wrote:

                        it's all coming to 1 byte file.

                        I hope that there's more than one byte in the database?

                        Bastard Programmer from Hell :suss: if you can't read my code, try converting it here[^]

                        J Offline
                        J Offline
                        Jassim Rahma
                        wrote on last edited by
                        #14

                        the field in the database is binary datatype with 255 length

                        L L 2 Replies Last reply
                        0
                        • J Jassim Rahma

                          the field in the database is binary datatype with 255 length

                          L Offline
                          L Offline
                          Lost User
                          wrote on last edited by
                          #15

                          255 byte? Sounds a bit small. Manual is talking about a LONGBLOB[^].

                          Bastard Programmer from Hell :suss: if you can't read my code, try converting it here[^]

                          1 Reply Last reply
                          0
                          • J Jassim Rahma

                            the field in the database is binary datatype with 255 length

                            L Offline
                            L Offline
                            Luc Pattyn
                            wrote on last edited by
                            #16

                            1. I don't recall ever having seen a Word document that would fit in 255 bytes. I just created a Word document containing a single letter ("a"), saved it to disk, and found a file size of 29KB. That was Word 2007 BTW. 2. The only type that is suited for storing binary data IMO is a "blob". MySQL offers blob, and some size variants thereof. Use those. I never used "binary". 3. Yes, saving and retrieving data to/from a database is tricky; as long as it doesn't work, it is hard to tell where the problem lies; it could be in the saving part, or in the retrieving part. And when you have several bugs at once (I'm sure you do!) fixing any one of them doesn't seem to help at all, until you get to the last one. The good thing is, you have to solve it only once, as it would apply to any kind of data, as long as it fits a byte array model, it is all the same. And the best thing is, millions of people have done this before, so the solution is bound to be available everywhere you look. :)

                            Luc Pattyn [My Articles] Nil Volentibus Arduum

                            J 1 Reply Last reply
                            0
                            • L Luc Pattyn

                              1. I don't recall ever having seen a Word document that would fit in 255 bytes. I just created a Word document containing a single letter ("a"), saved it to disk, and found a file size of 29KB. That was Word 2007 BTW. 2. The only type that is suited for storing binary data IMO is a "blob". MySQL offers blob, and some size variants thereof. Use those. I never used "binary". 3. Yes, saving and retrieving data to/from a database is tricky; as long as it doesn't work, it is hard to tell where the problem lies; it could be in the saving part, or in the retrieving part. And when you have several bugs at once (I'm sure you do!) fixing any one of them doesn't seem to help at all, until you get to the last one. The good thing is, you have to solve it only once, as it would apply to any kind of data, as long as it fits a byte array model, it is all the same. And the best thing is, millions of people have done this before, so the solution is bound to be available everywhere you look. :)

                              Luc Pattyn [My Articles] Nil Volentibus Arduum

                              J Offline
                              J Offline
                              Jassim Rahma
                              wrote on last edited by
                              #17

                              I changed to BLOB. now it's working for small files but when i try larger files it doesn't work i tried an image with 34kb and it was fine i tried an image with 118 kb but half of the image was black when i downloaded it i tried an image with 1MB but it was not downloaded properly.. less than 100 kb were downloaded from the file with no error!

                              L 1 Reply Last reply
                              0
                              • J Jassim Rahma

                                I changed to BLOB. now it's working for small files but when i try larger files it doesn't work i tried an image with 34kb and it was fine i tried an image with 118 kb but half of the image was black when i downloaded it i tried an image with 1MB but it was not downloaded properly.. less than 100 kb were downloaded from the file with no error!

                                L Offline
                                L Offline
                                Luc Pattyn
                                wrote on last edited by
                                #18

                                Thank you. :)

                                Luc Pattyn [My Articles] Nil Volentibus Arduum

                                J 1 Reply Last reply
                                0
                                • L Luc Pattyn

                                  Thank you. :)

                                  Luc Pattyn [My Articles] Nil Volentibus Arduum

                                  J Offline
                                  J Offline
                                  Jassim Rahma
                                  wrote on last edited by
                                  #19

                                  BIG thank you But........ Still having a problem.. :)

                                  L 1 Reply Last reply
                                  0
                                  • J Jassim Rahma

                                    BIG thank you But........ Still having a problem.. :)

                                    L Offline
                                    L Offline
                                    Lost User
                                    wrote on last edited by
                                    #20

                                    :) Current code & message.

                                    Bastard Programmer from Hell :suss: if you can't read my code, try converting it here[^]

                                    1 Reply Last reply
                                    0
                                    Reply
                                    • Reply as topic
                                    Log in to reply
                                    • Oldest to Newest
                                    • Newest to Oldest
                                    • Most Votes


                                    • Login

                                    • Don't have an account? Register

                                    • Login or register to search.
                                    • First post
                                      Last post
                                    0
                                    • Categories
                                    • Recent
                                    • Tags
                                    • Popular
                                    • World
                                    • Users
                                    • Groups