0

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?

1
  • you append image base64 string into URL,So this issue raised. Try to send base64 string as querystring to body
    – Kabilu
    Commented Jun 7 at 14:30

1 Answer 1

0

You are trying to include all the image data as part of the URI, and that is just not the way to do it. Images may be several megabytes, and URIs have a max length much shorter than that.

If you are uploading and image you should probably be using POST rather than GET.

You should also really not accept SQL command strings from the client, that is just begging for SQL injection attacks. Even if this is in a trusted environment it is just very poor practice.

You seem to be mangling the image. An Image can be represented as either an file, using some file format, or raw pixel data. If you use the later you also need to include some metadata about size, pixel format etc. Whatever you are doing to the image will not work. It should be much easier, faster, and correct, to just take the file bytes and pass along them directly.

Perhaps something like:

var fileBytes = await File.ReadAllBytesAsync(opf.FileName)
using var fileContent = new ByteArrayContent(fileBytes);
using HttpResponseMessage response = await httpClient.PostAsync("UpdateImage2", fileContent );

you would naturally need to change the server to accept the byteContent instead of a string, and do whatever it is you intend to do with it. You probably also want to check the file size, and that the file is in a white listed image formats before doing anything with it.

3
  • Thanks for your precious comments. I am new to ASP.Net and your comments were really helpful. I changed the client side as this: ' byte[] fileBytes = File.ReadAllBytes(opf.FileName); ByteArrayContent fileContent = new ByteArrayContent(fileBytes); HttpResponseMessage response = await Program.client.PostAsync("UpdateImage2", fileContent); ' But it returns "Internal server error after running last line. The problem is it does not enter in debug mode to see what happened to it Commented Jun 8 at 5:00
  • @user12235025, you can try to use Fiddler to capture the http request and check whether the data is in the request, and on the server side, you can also add log to check whether you can get the imge data or not? If the byte array can't send to webservice, you can transfer the image via Base64String and use StringContent, instead of ByteArrayContent.
    – Zhi Lv
    Commented Jun 10 at 3:51
  • @user12235025 "Internal server error" likely means an exception in your server code. I would recommend running your server in a debugger, place a break point at the top of the method, and step thru the code until you get the exception.
    – JonasH
    Commented Jun 10 at 6:27

Not the answer you're looking for? Browse other questions tagged or ask your own question.