hi
can anyone tell me.how can i convert the file into byte array.
Thank you
Loading
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Krishna GaradRank #191 · 12.8K reputationPosted Jan 28, 2011, 11:12 PM EST
In database create procedure
Create Procedure Sp_InsertFile
(
@resume VarBinary(max)
)
As
Begin
Insert Into Sample(Resume)Values(@resume)
End
Now in .cs file
if(FileUpload1.Hasfile)
{
BinaryReader br = new BinaryReader(FileUpload1.PostedFile.InputStream);
byte[] buffer = br.ReadBytes(FileUpload1.PostedFile.ContentLength);
string strconn = @"data source=system-7\sqlexpress;initial
catalog=jobs;uid=sa;pwd=as";
SqlConnection conn = new SqlConnection(strconn);
conn.Open();
SqlCommand cmd = new SqlCommand("Sp_InsertFile", conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@resume", SqlDbType.VarBinary);
cmd.Parameters[0].Value = buffer;
cmd.ExecuteNonQuery();
Response.Write("");
cmd.Dispose();
conn.Close();
}
And while Executing put a break point and check exact what kind of error is it. Because both code work fine.
Krishna GaradRank #191 · 12.8K reputationPosted Jan 23, 2011, 7:28 PM EST
BinaryReader br = new BinaryReader(FileUpload1.PostedFile.InputStream);
byte[] buffer = br.ReadBytes(FileUpload1.PostedFile.ContentLength);
krithika muthukrishnanRank #8,369 · 223 reputationPosted Jan 27, 2011, 8:06 PM EST
i wish to know how to convert the bytearray to a file,
thank you
Krishna GaradRank #191 · 12.8K reputationPosted Jan 27, 2011, 8:16 PM EST
krithika muthukrishnanRank #8,369 · 223 reputationPosted Jan 27, 2011, 11:17 PM EST
when i use this coding,i have an error in this line ,i choose the file from D:\.but it will show the error like could not find file in C:\.how to solve this.
coding :
FileStream fs = new FileStream(FileName, FileMode.Open,FileAccess.Read);
Error :
Could not find file 'C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\time.doc'.
Krishna GaradRank #191 · 12.8K reputationPosted Jan 27, 2011, 11:21 PM EST
krithika muthukrishnanRank #8,369 · 223 reputationPosted Jan 27, 2011, 11:31 PM EST
Krishna GaradRank #191 · 12.8K reputationPosted Jan 27, 2011, 11:35 PM EST
byte[] buffer;
if(FileUpload1.HasFile)
{
BinaryReader br = new BinaryReader(FileUpload1.PostedFile.InputStream);
byte[] buffer = br.ReadBytes(FileUpload1.PostedFile.ContentLength);
// Insert this byte array now to database as buffer.
}
Suthish NairRank #92 · 22.8K reputationPosted Jan 27, 2011, 11:49 PM EST
krithika muthukrishnanRank #8,369 · 223 reputationPosted Jan 27, 2011, 11:59 PM EST
in sql table i create the field for store byte array in a data type varbinary(max).when i insert the bytearray buffer to the sql table ,I have an error like
Error : Implicit conversion from data type varchar to varbinary(max) is not allowed. Use the CONVERT function to run this query.
Krishna GaradRank #191 · 12.8K reputationPosted Jan 28, 2011, 12:11 AM EST
In aspx.cs where you are uploading event happens
byte[] buffer;
if(FileUpload1.HasFile)
{
BinaryReader br = new BinaryReader(FileUpload1.PostedFile.InputStream);
byte[] buffer = br.ReadBytes(FileUpload1.PostedFile.ContentLength);
// Insert this byte array now to database as buffer.
}
In Insert Method
Insert File (byte[] filearray)
{
SqlParameter[] Param=new SqlParameter[0];
Param[0] = new SqlParameter("@image", SqlDbType.Image);
Param[0].Value = filearray;
//further insert to database attach this parameter to your command.
}
krithika muthukrishnanRank #8,369 · 223 reputationPosted Jan 28, 2011, 12:51 AM EST
ERror: Index was outside the bounds of the array.
Krishna GaradRank #191 · 12.8K reputationPosted Jan 28, 2011, 12:54 AM EST
krithika muthukrishnanRank #8,369 · 223 reputationPosted Jan 28, 2011, 12:58 AM EST
BinaryReader br = new BinaryReader(FileUpload1.PostedFile.InputStream);
byte[] buffer = br.ReadBytes(FileUpload1.PostedFile.ContentLength);Response.Write(buffer);
SqlParameter[] Param=new SqlParameter[0];
Param[0] = new SqlParameter("@image", SqlDbType.Image);
Param[0].Value = buffer;//here the error will occur index was outside the range
Krishna GaradRank #191 · 12.8K reputationPosted Jan 28, 2011, 1:03 AM EST
SqlParameter[] Param=new SqlParameter[1];//this is the error
Param[0] = new SqlParameter("@image", SqlDbType.VarBinary);
Param[0].Value = buffer
increase the size of SqlParameter
Krishna GaradRank #191 · 12.8K reputationPosted Jan 28, 2011, 1:20 AM EST
krithika muthukrishnanRank #8,369 · 223 reputationPosted Jan 28, 2011, 1:28 AM EST
Implicit conversion from datatype varchar to varbinary is not allowed error will occur in cmd.executenonquery() line.
Krishna GaradRank #191 · 12.8K reputationPosted Jan 28, 2011, 1:30 AM EST
krithika muthukrishnanRank #8,369 · 223 reputationPosted Jan 28, 2011, 1:36 AM EST
public byte[] FileToByteArray(string FileName)
{
BinaryReader br = new BinaryReader(FileUpload1.PostedFile.InputStream);
byte[] buffer = br.ReadBytes(FileUpload1.PostedFile.ContentLength);
Response.Write(buffer);
SqlParameter[] Param=new SqlParameter[1];
Param[0] = new SqlParameter("@image", SqlDbType.Image);
Param[0].Value = buffer;
string strconn = @"data source=system-7\sqlexpress;initial catalog=jobs;uid=sa;pwd=as";
string query = "select * from register";
SqlConnection conn = new SqlConnection(strconn);
conn.Open();
SqlCommand cmd = new SqlCommand(query);
cmd.Connection = conn;
cmd.CommandText = ("insert into sample values('" + Param + "')");
cmd.ExecuteNonQuery();//Error occur in this line
Response.Write("");
cmd.Dispose();
conn.Close();
return buffer;
}
Krishna GaradRank #191 · 12.8K reputationPosted Jan 28, 2011, 1:46 AM EST
public byte[] FileToByteArray(string FileName)
{
BinaryReader br = new BinaryReader(FileUpload1.
byte[] buffer = br.ReadBytes(FileUpload1.
Response.Write(buffer);
string strconn = @"data source=system-7\sqlexpress;
string query = "select * from register";
SqlConnection conn = new SqlConnection(strconn);
conn.Open();
SqlCommand cmd = new SqlCommand(query);
cmd.Connection = conn;
cmd.CommandText = ("insert into sample(columnname) values(" +buffer + ")");
//add columname in place of columnname.
cmd.ExecuteNonQuery();//Error occur in this line
Response.Write("");
cmd.Dispose();
conn.Close();
return buffer;
}
krithika muthukrishnanRank #8,369 · 223 reputationPosted Jan 28, 2011, 1:52 AM EST
Krishna GaradRank #191 · 12.8K reputationPosted Jan 28, 2011, 2:10 AM EST
public byte[] FileToByteArray(string FileName)
{
BinaryReader br = new BinaryReader(FileUpload1.
byte[] buffer = br.ReadBytes(FileUpload1.
Response.Write(buffer);
string strconn = @"data source=system-7\sqlexpress;
//string query = "select * from register";
SqlConnection conn = new SqlConnection(strconn);
conn.Open();
SqlCommand cmd = new SqlCommand(); //Here you dont want to pass you select query
cmd.Connection = conn;
cmd.CommandText = ("insert into sample(columnname) values(" +buffer + ")");
//add columname in place of columnname.
cmd.ExecuteNonQuery();//Error occur in this line
Response.Write("");
cmd.Dispose();
conn.Close();
return buffer;
}
I tested it in my db also it's now working. let me know if not. and if fileupload hasfile then only it will create byte[].
krithika muthukrishnanRank #8,369 · 223 reputationPosted Jan 28, 2011, 2:40 AM EST
public byte[] FileToByteArray()
{
BinaryReader br = new BinaryReader(FileUpload1.PostedFile.InputStream);
byte[] buffer = br.ReadBytes(FileUpload1.PostedFile.ContentLength);
Response.Write(buffer);
string strconn = @"data source=system-7\sqlexpress;initial
catalog=jobs;uid=sa;pwd=as";
SqlConnection conn = new SqlConnection(strconn);
conn.Open();
string query = "insert into sample(resume) values('" + buffer + "')";
SqlCommand cmd = new SqlCommand(query, conn);
cmd.ExecuteNonQuery();
Response.Write("");
cmd.Dispose();
conn.Close();
return buffer;
}
Krishna GaradRank #191 · 12.8K reputationPosted Jan 28, 2011, 2:55 AM EST
else also write like this don't modify anything in this.
if (FileUpload1.HasFile)
{
BinaryReader br = new BinaryReader(FileUpload1.PostedFile.InputStream);
byte[] buffer = br.ReadBytes(FileUpload1.PostedFile.ContentLength);
Response.Write(buffer);
string strconn = @"data source=system-7\sqlexpress;
catalog=jobs;uid=sa;pwd=as";
SqlConnection conn = new SqlConnection(strconn);
conn.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandText = ("insert into sample(resume) values('" + buffer + "')");
cmd.ExecuteNonQuery();
Response.Write("");
cmd.Dispose();
conn.Close();
}
just copy and paste it's working. if it not worked again just restart your SQL Server and visual studio.
Suthish NairRank #92 · 22.8K reputationPosted Jan 28, 2011, 7:12 AM EST
krithika muthukrishnanRank #8,369 · 223 reputationPosted Jan 28, 2011, 6:50 PM EST
here i attached my code behind page.
Krishna GaradRank #191 · 12.8K reputationPosted Jan 28, 2011, 6:53 PM EST
krithika muthukrishnanRank #8,369 · 223 reputationPosted Jan 28, 2011, 7:23 PM EST
krithika muthukrishnanRank #8,369 · 223 reputationPosted Jan 28, 2011, 11:28 PM EST
but i dont know why the previous codings didn't give best answer.
Krishna GaradRank #191 · 12.8K reputationPosted Jan 28, 2011, 11:32 PM EST
krithika muthukrishnanRank #8,369 · 223 reputationPosted Jan 29, 2011, 12:24 AM EST
Suthish NairRank #92 · 22.8K reputationPosted Jan 29, 2011, 12:41 AM EST
Dont copy paste the code, will not helps you. try to understand how the code/procedure works.
Check below all links, if this didnt helps you so we cant.
Link1 Link2 Link3 Link4
Bharat Bhushan177 reputationPosted Jan 29, 2011, 2:04 AM EST
http://useofaspdotnet.blogspot.com/2011/01/how-to-encrypt-password-and-store-it-in.html
fds vgvf dfgv4 reputationPosted Apr 22, 2013, 8:13 PM EDT
here is an image to byte array. Imports System.IO Imports System.Drawing.Printing Imports RasterEdge.Imaging Imports RasterEdge.Imaging.Processing Imports RasterEdge.Imaging.Converting.ImageToByte Dim Image As New RasterEdgeImaging() If True Then Dim image As New MemoryStream() ImageID.Save(image, System.Drawing.Imaging.ImageFormat.Jpeg) Return ms.ToArray() End If Image.save()