DiigIT | IT Community
No Profile Image
Welcome Guest
New User? Register | Login

How to attach binary data to the end of a POST request

By: rekha singh | 20 Apr 2010 4:51 pm

Does anyone know how to attach binary data to the end of a POST request? There's an API that requires me to upload a file by sending a few POST variables in a REST call along with a 'source=<raw binary data of file>". How can I do that in VB.NET

Comments

What kind of binary file? an image file?, like when you saving an image to database your image file automatically convert it to binary numbers. when we are saving files to a BLOB (Binary Large Object) field. BLOB fields are fields which store large chunks of data of varying formats and indefinite size as opposed to simple data types. The BLOB column saves data as binary strings or byte strings and cannot be searched.

when you want to covert binary to image you have to use memory stream.
 
'to save image to database and convert it to binary
Dim ms As New MemoryStream
 
Me.PictureBox1.Image.Save(ms, Me.PictureBox1.Image.RawFormat)
 
 
Dim arrayImage() As Byte = ms.GetBuffer
 
 
ms.Close() ' Closes the Memory Stream
 
 
Dim nStr As String = Me.Label1.Text.Substring(Me.Label1.Text.LastIndexOf("\") + 1)
 
 
Dim strQuery As String = "INSERT INTO Pic(Name, Picture) VALUES(@Name, @Picture)"
 
 
Dim objcommand As New SqlCommand(strQuery, Me.SqlConnection1)
 
With objcommand
 
.Parameters.Add(New SqlParameter("@Name", SqlDbType.NVarChar, 50)).Value = nStr
 
.Parameters.Add(New SqlParameter("@Picture", SqlDbType.Image)).Value = arrayImage
 
End With
 
 
Me.SqlConnection1.Open()
 
objcommand.ExecuteNonQuery()
 
 
MessageBox.Show("Image Saved Into the DataBase")
 
 
Me.SqlConnection1.Close()
 
' to dispaly binary and convert it to image
 
Dim arrayImage() As Byte = CType(Me.DataSet11.Tables(0).Rows(Me.ListBox1.SelectedIndex)("Picture"), Byte())
 
Dim ms As New MemoryStream(arrayImage)
 
With Me.PictureBox1
 
.Image = Image.FromStream(ms)
 
.SizeMode = PictureBoxSizeMode.CenterImage
End With
 
i'm not sure if this is your looking for, but i hope the code give you a little bit idea. good luck!
 
By: rekha singh | 20 Apr 2010

Leave a comment

Enter the text in the image
img
Can't read?
Type the characters you see in the picture below.


Close Move