1

I want to upload image to IIS server using c# web service. I have written the web method for this as follows:

[WebMethod] 
public string UploadFile(byte[] f, string fileName)
{
    try
    {
       MemoryStream ms = new MemoryStream(f);

       FileStream fs = new FileStream(System.Web.Hosting.HostingEnvironment.MapPath
       ("~/TransientStorage/") +fileName, FileMode.Create); 

       ms.WriteTo(fs); 

       ms.Close();
       fs.Close();
       fs.Dispose(); 

       return "OK";
    }
    catch (Exception ex)
    {
        // return the error message if the operation fails
        return ex.Message.ToString();
    }
}

Here the web method is taking argument as byte[] .I have convert the sdcard image to byte[] but when i am passing it as an URL paramete it is not working .I have also tried by converting the byte[] array to base64 strig still it is not working.

Can any one tell me how to upload image to IIS server using c# web service.

1 Answer 1

4

You'll need to POST it to the server, setting the content type and including the byte array as the body of the post.

See here for an example of doing it from C# and here for an example for Android.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks a lot Andy... the code is working fine but after uploading i can't find the destination folder of the uploaded images. i am running php on IIS server.
Glad it helped. The uploaded file should be in the web service application folder under the TransientStorage folder? Does the folder exist? Are you getting any errors returned? Are you able to debug the web service? I'm also a little bit confused where php comes into this setup?
Thanks for the link its very useful. i understood how the byte[] will be sent, where am i sending the filename?? and why do we do this piece of code outputStream.writeBytes(twoHyphens + boundary + lineEnd); outputStream.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\"" + pathToOurFile +"\"" + lineEnd); outputStream.writeBytes(lineEnd); if i have content with my image whats the best way to send it ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.