In most of applications, now a days, image uploading is a common job. And there are many ways to upload an image on a server or in a database. This article explains how to upload an Image in byte format in a database using image streaming.
Here, I have created a REST API in WCF to upload an image and one ASP.Net web application to consume that API. I have attached a solution file to this article and you can download and try it on your machine.
Here I have pasted the code to upload an image in byte format in a database. The image should be posted in stream format on the service.
API implementation
I assume you are familiar with WCF services, so I will not cover that in this article. So first of all, we need to declare an operation contract method with ResponseFormat, UriTemplate and Method in Interface to expose the API.
- [ServiceContract]
- public interface IService1
- {
- [OperationContract]
- [WebInvoke(ResponseFormat = WebMessageFormat.Json, UriTemplate = "PostImage", Method = "POST")]
- string PostImage(Stream sm);
-
- }
After this, we need to write functionality for the preceding method in the Service file (.svc).
- public class Service1: IService1
- {
-
- public string PostImage(Stream sm)
- {
- System.Drawing.Bitmap imag = new System.Drawing.Bitmap(sm);
- byte[] imagedata = ImageToByte(imag);
-
- return "success";
- }
-
- public static byte[] ImageToByte(System.Drawing.Image img)
- {
-
- ImageConverter converter = new ImageConverter();
- return (byte[])converter.ConvertTo(img, typeof(byte[]));
- }
- }
You need to host your service on the server (IIS or which one you prefer). And provide that service location URL in the client-side application.
Client-Side application
Now we need to create a web application to consume the API. I am giving here the method functionality that will post an image on the server using the REST API. You can call this method on any button click or File change event or on any event that should upload the image.
- public void PostData()
- {
- try
- {
- byte[] fileToSend = null;
- string name = "";
-
- if (FileUpload1.HasFile)
- {
- name = FileUpload1.FileName;
- Stream stream = FileUpload1.FileContent;
- stream.Seek(0, SeekOrigin.Begin);
- fileToSend = new byte[stream.Length];
- int count = 0;
- while (count < stream.Length)
- {
- fileToSend[count++] = Convert.ToByte(stream.ReadByte());
- }
-
- }
- //Here provide your service location url in below line. You need to host your service on server(IIS or which one you prefer)
- HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create
- ("http://localhost:63624/Service1.svc/PostImage");
-
- req.Method = "POST";
- req.ContentType = "application/octet-stream";
- req.ContentLength = fileToSend.Length;
- Stream reqStream = req.GetRequestStream();
- reqStream.Write(fileToSend, 0, fileToSend.Length);
- reqStream.Close();
-
- HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
-
- StreamReader reader = new StreamReader(resp.GetResponseStream());
-
- string result = reader.ReadToEnd();
- }
- catch (Exception ex)
- {
-
- throw ex;
- }
- }
That's all we must do. You can download the attached file and try it yourself. In the attached file, WebApplication1 is a client-side application that consumes the service. You can look at the Test.aspx page and feel free to ask any question regarding this.