locked
POST stream data in REST API-WCf RRS feed

  • Question

  • User-792764019 posted

    Hi,

            I am working on a wcf rest api. I got issue to upload images from Client side to API. From Client side I do POST method to pass Streaming, and In Service, trying to fetch data through Message, StreamReader and some other ways also tried.

    I checked in debugging mode, stream has length of 15000 in Client Side, and when I recieve it in service, it is more than 15000, it is 21000.

    I am not getting any idea why some extra values are added in Stream.

    Please some one give idea to solve this issue.

    Thanks

    VD

    Friday, January 4, 2013 9:34 PM

Answers

  • User-792764019 posted

    Finally I got the solution.

    WCF REST API...
    
    
    public DealerImage PostImage(Stream sm)
             {
               System.Drawing.Bitmap imag = new System.Drawing.Bitmap(sm);
               byte[] imagedata = ImageToByte(imag);
                //Save byte code to database..
              
            }
    
           public static byte[] ImageToByte(System.Drawing.Image img)
            {
               
                ImageConverter converter = new ImageConverter();
                return (byte[])converter.ConvertTo(img, typeof(byte[]));
            }
    
    Client Code to POST 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());
                }
                
            }
    
            HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create("URL here");
          
            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;
            }
        }
    
    
    
    

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, January 9, 2013 2:40 PM

All replies

  • User-1662538993 posted

    Check this article-

    http://code.msdn.microsoft.com/windowsdesktop/Upload-files-using-a-REST-13f16af2

    Check this in your config file-

    maxStringContentLength and maxBytesPerRead

    and make it to maximum for the testing purpost.

    Monday, January 7, 2013 8:57 AM
  • User-792764019 posted

    Finally I got the solution.

    WCF REST API...
    
    
    public DealerImage PostImage(Stream sm)
             {
               System.Drawing.Bitmap imag = new System.Drawing.Bitmap(sm);
               byte[] imagedata = ImageToByte(imag);
                //Save byte code to database..
              
            }
    
           public static byte[] ImageToByte(System.Drawing.Image img)
            {
               
                ImageConverter converter = new ImageConverter();
                return (byte[])converter.ConvertTo(img, typeof(byte[]));
            }
    
    Client Code to POST 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());
                }
                
            }
    
            HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create("URL here");
          
            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;
            }
        }
    
    
    
    

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, January 9, 2013 2:40 PM