Problem saving file to MySQL
-
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.
the VARCHAR was for the document format which is text representing the file extension. The document itself is the param_resume_data...
-
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;
} -
the VARCHAR was for the document format which is text representing the file extension. The document itself is the param_resume_data...
-
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'
-
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'
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[^]
-
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[^]
the field in the database is binary datatype with 255 length
-
the field in the database is binary datatype with 255 length
-
the field in the database is binary datatype with 255 length
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
-
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
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!
-
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!
Thank you. :)
Luc Pattyn [My Articles] Nil Volentibus Arduum
-
Thank you. :)
Luc Pattyn [My Articles] Nil Volentibus Arduum
BIG thank you But........ Still having a problem.. :)
-
BIG thank you But........ Still having a problem.. :)