1

I am working on an API kind of project,

I have wrote a WebMethod (not exactly. I am using MVC to create REST like API)

public UploadFileImage(string employeeId, byte[] imageBytes, string imageName)
{
    // saves the imagebyte as an image to a folder
}

the web service would be consumed by a web app, or windows or even iphone or such portable stuffs. I am testing my web service using a web app, by simple httpPost.

string Post(Uri RequestUri, string Data)
    {
        try
        {
            HttpWebRequest request = HttpWebRequest.Create(RequestUri) as HttpWebRequest;

            request.Method = "POST";

            request.ContentType = IsXml.Checked ? "text/xml" : "application/x-www-form-urlencoded";

            byte[] bytes = Encoding.ASCII.GetBytes(Data);
            Stream os = null; // send the Post
            request.ContentLength = bytes.Length;   //Count bytes to send
            os = request.GetRequestStream();
            os.Write(bytes, 0, bytes.Length);

            HttpWebResponse httpWebResponse = (HttpWebResponse)request.GetResponse();
            StreamReader streamReader = new StreamReader(request.GetResponse().GetResponseStream());
            return streamReader.ReadToEnd();

        }
        catch (Exception ex)
        {
            return ex.Message;
        }
    }

This code works fine for evey method like, AddEmployee, DeleteEmployee etc. THe parameter Data is of form "Id=123&name=abcdefgh&desig=Developer",

How I call any other function is Post(new Uri("http://localhost/addemployee"),"name=abcd&password=efgh")
where post is the function i wrote.

All good for all functions. Except that I dont know how to consume the above mentioned function UploadFileImage to upload an image?

Thanks

skaffman
  • 398,947
  • 96
  • 818
  • 769
eMan
  • 45
  • 6

2 Answers2

3

Try encoding the imageBytes as Base64.

Richard Schneider
  • 34,944
  • 9
  • 57
  • 73
  • Did you mean to use Convert.ToBase64String(fileByteArray), where fileByteArray is the bytes of the file. – eMan May 18 '11 at 06:51
  • thanks, thats working, while posting, post it as base64 encoded string, and in webservice, decode it. – eMan May 18 '11 at 08:27
0

From your code snippet is not too clear how you call UploadFileImage, that is how you convert its parameters tripplet into Data.

That is why my answer is quite generic:

In general, you'd better transfer your image file by

request.ContentType = "multipart/form-data; boundary=----------------------------" + DateTime.Now.Ticks.ToString("x");

Please allow me to refer you to a sample at StackOverflow on how to format a multipart request. I am sure that if you google, you shall find a lots of detailed examples and explanations as well.

I hope this helps :-)

Community
  • 1
  • 1
Lev
  • 287
  • 3
  • 13
  • thanks for the reply. I have another function in my webservice (mvc controller) like "AddEmployee". How I call is, to do a simple httppost to the url. Post(new Uri("http://localhost/addemployee"),"name=abcd&password=efgh") where post is the function i wrote – eMan May 18 '11 at 06:45
  • You welcome :-) As I wrote the interesting part about how you invoke the _Post()_ method is its second parameter - _string Data_. When you pass _employeeId_, _imageBytes_ and _imageName_, how are they mapped to _Data_? – Lev May 18 '11 at 07:19
  • :) that is what my question is all about. "All good for all functions. Except that I dont know how to consume the above mentioned function UploadFileImage to upload an image?" ie, for AddEmployee and other functions, i can say Data ="name=abcd&password=efgh". But how I will do it for UploadFileImage – eMan May 18 '11 at 07:35
  • I am trying to get a solution combining both the replies i got, ie @Richard Schneider and the link you posted – eMan May 18 '11 at 07:46