How do I upload a file, process it and return a result file in a single request to a REST WCF service?

ⅰ亾dé卋堺 提交于 2020-01-16 05:09:06

问题


I need to implement the following scenario in a REST service implemented in WCF:

  • the user submits a binary file and a set of parameters
  • 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?


回答1:


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.




回答2:


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>


来源:https://stackoverflow.com/questions/4711811/how-do-i-upload-a-file-process-it-and-return-a-result-file-in-a-single-request

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!