I want to send image itself to a SQL Server on localhost (send an image from my computer to localhost using ASP.NET Core web service written in C#).
I tried this in my client side:
OpenFileDialog opf = new OpenFileDialog();
Image img = Image.FromFile(opf.FileName);
MemoryStream tmpStream = new MemoryStream();
img.Save(tmpStream, System.Drawing.Imaging.ImageFormat.Png);
tmpStream.Seek(0, SeekOrigin.Begin);
byte[] imageData = new byte[img.Size.Height * img.Height];
tmpStream.Read(imageData, 0, img.Size.Height * img.Height);
// string UpdateString = "UPDATE CompSett SET Logo=@image";
string Base64String = "data:image/jpeg;base64," + Convert.ToBase64String(imageData, 0, imageData.Length);
string Image2 = JsonConvert.SerializeObject(imageData);
HttpResponseMessage message = Program.client.GetAsync("UpdateImage2?UpdateString=" + UpdateString + "&image=" + Base64String).Result;
And this is my code on the server side:
private static SqlConnection connection = new SqlConnection();
[WebMethod]
public string UpdateImage2(string UpdateString, string image)
{
string ConnString = "Data Source=(local);Initial Catalog=HA;Integrated Security=True";
connection.ConnectionString = ConnString;
string response = string.Empty;
SqlCommand cmd = new SqlCommand();
cmd.CommandText = UpdateString;
cmd.Parameters.Clear();
cmd.Parameters.Add(new SqlParameter("@image", image));
cmd.Connection = connection;
connection.Open();
int i = cmd.ExecuteNonQuery();
if (i > 0)
{
response = "Record updated";
}
else
response = "Error";
connection.Close();
return response;
}
If I pass image as string (as shown in the code), the string that I get in my web method is System.Byte[]
the total byte of image.
On the other hand, if I send image as byte, I get the following error:
Invalid URI: The Uri string is too long HttpGET
Would you please tell me how to fix it?