0

I have implemented a Restful web service in C# .Net and I want to enable users to upload images via this.

What I currently have is a service that only accepts text data. Ideally I need to get an image from the user and store it in my database.

[OperationContract]
[WebInvoke(UriTemplate = "/{key}", Method = "POST")]
ResponseStatus InsertSensor(string key, Image image);

My question what is the most efficient way to do this?

Many Thanks,

0

3 Answers 3

1

An image is not something you can transmit down a wire.

Instead, you need to encode the image into a stream or byte array (probably using PNG or JPEG).

The client would then send these bytes to the server, and the server would store it in the database.

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

6 Comments

What are the recommended ways to encode an image can I use XML?
@DafaDil: XML is a very inefficient way to encode an image. You are probably familiar with existing standard image encodings, such as PNG, GIF, or JPEG. Note that each one has its strengths and weaknesses.
What do we need to pass when calling the service? is it the image path? Ex: - serviceAPi/10/c:/Images/image1.jpg, and what is the type of the input parameter?
@DafaDil: The web service has no access to your client disk. You need to pass the actual bytes as a POST payload.
So it would be something like a byte[] buffer?
|
1

Sample project from MSDN: ASP.NET Web API: File Upload and Multipart MIME

Here's another write-up that might be useful: http://blogs.msdn.com/b/henrikn/archive/2012/03/01/file-upload-and-asp-net-web-api.aspx

1 Comment

What do we need to pass when calling the service? is it the image path? Ex: - serviceAPi/10/c:/Images/image1.jpg, and what is the type of the input parameter?
1

You could allow the users to post the image to a http handler as described in this question.

1 Comment

What do we need to pass when calling the service? is it the image path? Ex: - serviceAPi/10/c:/Images/image1.jpg, and what is the type of the input parameter?

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.