简体   繁体   English

WCF RESTful服务-上载两个文件

[英]WCF RESTful service - upload two files

I need to upload two files with WCF REST. 我需要使用WCF REST上传两个文件。 This WCF Restful service file upload with multi-platform support works for me when uploading single file, but when I try something like 载单个文件时,这种具有多平台支持的WCF Restful服务文件上载对我有用,但是当我尝试类似的操作时

[WebInvoke(Method = "POST", UriTemplate = "FileUpload/{file1Name};{file2Name}")]

it fails. 它失败。 So the question is: How to put two arguments into Uri? 所以问题是:如何将两个参数放入Uri?

Thanks in advance, JP 在此先感谢JP

The answer to the linked question deals with uploading a single file - and that's fairly simple, because the service will read the entire HTTP request body, and that will be bound to the stream parameter. 链接问题的答案涉及上载单个文件-这很简单,因为该服务将读取整个HTTP请求正文,并将其绑定到stream参数。 When you're dealing with two (or more files) this doesn't work - you need some delimiter to be able to know when one file ends and the other starts. 当您处理两个(或多个)文件时,此操作不起作用-您需要一些定界符才能知道一个文件何时结束而另一个文件何时开始。 There are a few media types which handle this for that (multipart/* being the most common), but they're not supported natively in WCF (you'd either need to create a custom formatter to deal with it, or take the whole body as a stream and split the files in the FileUpload operation). 有几种媒体类型可以处理此问题(multipart / *是最常见的),但是WCF本身不支持它们(您需要创建一个自定义格式器来处理它,或者将其全部主体作为流,并在FileUpload操作中拆分文件)。

Now assuming that you have a way to receive two files in the request body, we can go to your specific question. 现在假设您有一种方法可以在请求正文中接收两个文件,我们可以转到您的特定问题。 The URI template feature allows you to pass multiple parameters, but they cannot be in the same "part" of the URI path. URI模板功能允许您传递多个参数,但是它们不能位于URI路径的同一“部分”中。 One option would be to split them in multiple parts: 一种选择是将它们分成多个部分:

[WebInvoke(UriTemplate = "FileUpload/{file1Name}/{file2Name}")]

Or use query parameters for that: 或使用查询参数:

[WebInvoke(UriTemplate = "FileUpload?file1={file1Name}&file2={file2Name}")]

That should do the job. 那应该做的。 Another alternative, since you already have to split the files on the input stream anyway, would be to use a format which allows you to specify the names in the stream itself. 另一个选择是,由于您已经必须拆分输入流上的文件,因此将使用一种格式,该格式允许您在流本身中指定名称。 For example, this would be one alternative: 例如,这将是一种替代方法:

<length of first file name, in bytes>
<first fileName>
<length of the first file, in bytes>
<bytes corresponding to the first file>
<length of second file name, in bytes>
<second fileName>
<length of the second file, in bytes>
<bytes corresponding to the second file>
00 (or any other value indicating the end of the files)

There are many others (including multipart/*), and in all of them you'll need to somehow parse the input to take the multiple files apart. 还有许多其他文件(包括multipart / *),在所有这些文件中,您都需要以某种方式解析输入以将多个文件分开。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM