C# 使用 HttpRequestMessage 或 Stream 上传 REST 文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9290342/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
REST file upload with HttpRequestMessage or Stream?
提问by Remy
What is the better way to upload a file for a REST client?
为 REST 客户端上传文件的更好方法是什么?
From the WCF Web API Documentation
来自 WCF Web API 文档
[WebInvoke(UriTemplate = "thumbnail", Method = "POST")]
public HttpResponseMessage UploadFile(HttpRequestMessage request)
{
From multiple forum posts:
WCF REST File upload with additional parameters
来自多个论坛帖子:
WCF REST File upload with additional parameters
[WebGet(UriTemplate="", Method ="POST"]
public string UploadFile(Stream fileContents)
I understand, that the first method allows to directly post a file from a normal HTML form. The 2nd approach seems more common on all forum posts I find.
我知道,第一种方法允许直接从普通的 HTML 表单发布文件。在我发现的所有论坛帖子中,第二种方法似乎更常见。
What would you recommend and why? The REST api should be accessible from all kind of languages and platforms.
你会推荐什么,为什么?REST api 应该可以从所有类型的语言和平台访问。
For the HttpRequestMessage approach, how would I do an upload a file preferable with the WCF HttpClient? With the FormUrlEncodedMediaTypeFormatter)
对于 HttpRequestMessage 方法,我将如何上传更适合使用 WCF HttpClient 的文件?使用 FormUrlEncodedMediaTypeFormatter)
采纳答案by JCaffeine
The first method is "closer to the metal" and would be more flexible since you would be processing the http requests and building the responses yourself. If all you need to do is accept a stream from a client, the second option is much simpler from the implementation standpoint (under the hood, it does the same work that the first method is doing)
第一种方法“更接近金属”并且会更灵活,因为您将自己处理 http 请求并构建响应。如果您需要做的只是接受来自客户端的流,那么从实现的角度来看,第二个选项要简单得多(在幕后,它与第一个方法所做的工作相同)
I don't have an answer for your last question.
你最后一个问题我没有答案。
回答by Jed
In order to test the HttpRequestMessage approach I have done the following using MVC:
为了测试 HttpRequestMessage 方法,我使用 MVC 完成了以下操作:
public class TestingController : Controller
{
public ActionResult Index()
{
return View();
}
public ActionResult Upload()
{
var file = Request.Files[0];
var filename = Request.Form["filename"];
var uri = string.Format("http://yoururl/serviceRoute/{0}", filename);
var client = new HttpClient();
client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("image/pjpeg"));
var content = new StreamContent(file.InputStream);
var response = client.PostAsync(uri, content);
ViewBag.ServerUri = uri;
ViewBag.StatusCode = response.Result.StatusCode.ToString();
return View();
}
}
The Index view should have a form in it that posts back to the Upload method. Then you are able to use the HttpClient to make a connection to your REST service.
Index 视图中应该有一个表单,可以回发到 Upload 方法。然后您就可以使用 HttpClient 连接到您的 REST 服务。

