简体   繁体   English

如何从ASP.NET Web API发布请求中读取文件数据?

[英]How can I read the file data from an ASP.NET web API post request?

Currently I have a "Post" action in my web API controller. 目前,我的Web API控制器中有一个“发布”操作。 I first attempted having a byte[] as the only argument, hoping it would "just work". 我首先尝试将byte[]作为唯一参数,希望它可以“正常工作”。 Unfortunately, it did not and it complained about formatting and conversion for form-data . 不幸的是,它没有这样做,它抱怨form-data格式和转换。

Now I have attempted to read the file content with this.Request.Content , which others seem to have had success with. 现在,我尝试使用this.Request.Content读取文件内容,其他人似乎已经成功读取了文件内容。 Unfortunately this is including more than I really want: The content disposition, the file name, the content type, and some kind of webkit form boundary. 不幸的是,这包括了超出我真正想要的范围:内容配置,文件名,内容类型和某种Webkit表单边界。 I assume it is all of the request content. 我认为这就是所有要求的内容。

What is the right way to do this? 什么是正确的方法? I have found lots of answers for ASP .NET with the FileUpload control and many articles on how to upload to a .NET web API expecting a file via POST . 我通过FileUpload控件找到了ASP .NET的许多答案,以及许多有关如何通过POST将文件上传到期望文件的.NET Web API的文章。

Some articles mention Request.Files but it is not part of the HttpRequestMessage class. 有些文章提到Request.Files但它不是HttpRequestMessage类的一部分。

If you're talking about multipart form data, you should have a look at our official documentation: 如果您要讨论多部分表单数据,则应查看我们的官方文档:

http://www.asp.net/web-api/overview/working-with-http/sending-html-form-data,-part-2 http://www.asp.net/web-api/overview/working-with-http/sending-html-form-data,-part-2

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

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