如何从ASP.NET Web API(POST或PUT)调用获取httppostedfile? [英] how to get a httppostedfile from a ASP.NET Web API (POST or PUT) call?

查看:151
本文介绍了如何从ASP.NET Web API(POST或PUT)调用获取httppostedfile?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

其实我的问题很简短。

如何从ASP.NET Web API POST或PUT获取HttpPostedFile?

How can I get a HttpPostedFile from a ASP.NET Web API POST or PUT?

我确实看到我可以从请求中获取各种信息,如Request.Header,Request.Content,Request.Properties。在哪里可以找到我传递的文件,如何从中创建HttpPostedFile?

I did see that I can get various information from the Request like Request.Header, Request.Content, Request.Properties. Where in there can I find the file I passed and how can I create a HttpPostedFile from it?

提前致谢!

推荐答案

我担心你的简短问题没有简短的答案。

Your short question does not have a short answer I am afraid.

ASP.NET Web API让你暴露ASP.NET MVC抽象了一些HTTP的奇迹 - 在这种情况下为HttpPostedFile。

ASP.NET Web API exposes you to the wonders of HTTP while ASP.NET MVC abstracted some of it - in this case for HttpPostedFile.

所以有点背景:

涉及文件的HTTP帖子通常有 multipart 表单数据内容。这意味着您正在混合使用不同类型的内容类型:您的普通表单数据将使用formurlencoded发送,而文件将通过application / octent-stream发送。

HTTP posts where a file is involved usually has multipart form data content. This means that you are mixing different kind of content-type: your normal form data will be sent using formurlencoded while the files will be sent application/octent-stream.

所以在Web API中,您所要做的就是说

So in Web API, all you have to do is to say

var contents = message.Content.ReadAsMultipartAsync(); // message is HttpRequestMessage

其中一个内容将包含您的文件。

One of the contents will contain your file.

这篇关于如何从ASP.NET Web API(POST或PUT)调用获取httppostedfile?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆