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);
- }
- public class Service1: IService1
- {
- public string PostImage(Stream sm)
- {
- System.Drawing.Bitmap imag = new System.Drawing.Bitmap(sm);
- byte[] imagedata = ImageToByte(imag);
- //Write code here to Save byte code to database..
- return "success";
- }
- public static byte[] ImageToByte(System.Drawing.Image img)
- {
- ImageConverter converter = new ImageConverter();
- return (byte[])converter.ConvertTo(img, typeof(byte[]));
- }
- }
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;
- }
- }

Prakash KhadkaPosted Aug 17, 2020, 12:11 PM
Does anyone have file upload restful api not the mvc to put image to the database.???
Vishwakant TripathiPosted Sep 10, 2018, 1:08 AM
Sankar Saha Your issue looks like request size problem. Can you please check by increasing request/buffer size of service.
Chamo DeePosted Jul 11, 2015, 1:56 AM
Thank you so much :)
Siddharth SPosted Apr 21, 2015, 8:13 AM
nice and easy explanation...thanks VD tripathi
Arvind SinghPosted Apr 18, 2015, 4:52 AM
Very good Article ..
Rahul Kumar SaxenaPosted Apr 14, 2015, 2:22 PM
Good Show...
Santhakumar MunuswamyPosted Apr 14, 2015, 12:22 PM
thanks for sharing
Santhakumar MunuswamyPosted Apr 14, 2015, 12:22 PM
Good work
Karthik Muthu KaruppanPosted Apr 14, 2015, 12:12 PM
Good one