Click here to Skip to main content
15,886,774 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have created a page for file upload control for converting 'nvarchar(max)' type to 'bytes' !
Its working now !
but I want to know more about this code convertion !

If I want to make conversion varbinary(max) to bytes , what should I do ?
C#
string filename = Path.GetFileName(FileUpload1.PostedFile.FileName);
string ext = Path.GetExtension(filename);
if (ext == ".png" || ext == ".jpg" || ext == ".jpeg" || ext == ".PNG" || ext == ".JPG" || ext == ".JPEG" || ext == ".gif" || ext == ".GIF")
{
    Stream fs = FileUpload1.PostedFile.InputStream;
    BinaryReader br = new BinaryReader(fs);
    Byte[] bytes = br.ReadBytes((Int32)fs.Length);
    string base64String = Convert.ToBase64String(bytes, 0, bytes.Length);
    Label1.Text = "Data stored successfully !!!!!";
}
else
{
    Response.Write("<script>alert('unsupported format of photo file');</script>");
}


Thanks in advance !
Posted
Updated 4-Mar-14 1:41am
v2
Comments
[no name] 4-Mar-14 7:54am    
Can you make clear that why you want to convert the varbinary(max) to bytes ? Varbinary(max) need to store the data of byte. So, if you want to store the byte in sql server then no need to convert the byte to varbinary. SQL Server can automatically store the byte value in varbinary field. you can get help from this link http://www.c-sharpcorner.com/Forums/Thread/60371/

1 solution

1. Saving an image's bytes as a Base64 encoding string is a WTF. Database engines offer binary column types. Better use them.
2. To get the bytes from such a "wrongly" stored image, use Convert.FromBase64String(your string).
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900