How do I upload a file, process it and return a result file in a single request to a REST WCF service?
I need to implement the following scenario in a REST service implemented in WCF:
- the user submits a binary file and a set of paramete开发者_如何转开发rs
- the server consumes the file, does some clever work and generates a binary output file
- the user retrieves that binary result file
and all that is done in a single operation from the client perspective.
It's pretty easy in a non-REST service. How do I do that in a REST service? Where do I get started?
The tricky part is that the XMLHttpRequest
object doesn't support file upload.
So common work arounds are using Flash or another plugin for uploading a file. Or uploading the file using an IFrame
.
There are many plugins that automate the process, for example http://jquery.malsup.com/form/#file-upload
The server part is not that difficult.
Your Request
will have a Files
attribute which contains the uploaded files. Save the files, and return whatever you want in the Response.
Check out the example in the above stated plugin for dealing with the response on the client side.
Since your linked question is not directly about streaming over HTTP you can of course send byte arrays in REST service. Here is example of service contract:
[ServiceContract]
public interface IService
{
[OperationContract]
[WebInvoke(Method = "POST")]
byte[] GetByteData(byte[] data);
}
Data will be send as base64 encoded string. Example of send message:
<base64Binary xmlns="http://schemas.microsoft.com/2003/10/Serialization/">QmFzZSA2NCBTdHJlYW0=</base64Binary>
精彩评论