asp.net - 如何在 .net web api Controller 中读取多部分表单数据

标签 asp.net .net asp.net-mvc asp.net-mvc-3 asp.net-web-api

public class Sampleontroller:apicontroller
 {    
    public void PostBodyMethod() {
        HttpRequestMessage request=this.request;
     //How to read the multi part data in the method
    }
}
我正在向 web api Controller 发送多部分数据。
如何读取方法中的内容?

最佳答案

“异步”示例:

public async Task<HttpResponseMessage> PostSurveys()
    {
        // Verify that this is an HTML Form file upload request
        if (!Request.Content.IsMimeMultipartContent("form-data"))
        {
            return Request.CreateResponse(HttpStatusCode.BadRequest);
        }

            //Destination folder
            string uploadFolder = "mydestinationfolder";

            // Create a stream provider for setting up output streams that saves the output under -uploadFolder-
            // If you want full control over how the stream is saved then derive from MultipartFormDataStreamProvider and override what you need.            
            MultipartFormDataStreamProvider streamProvider = new MultipartFormDataStreamProvider(uploadFolder );                
            MultipartFileStreamProvider multipartFileStreamProvider = await Request.Content.ReadAsMultipartAsync(streamProvider);

            // Get the file names.
            foreach (MultipartFileData file in streamProvider.FileData)
            {
                //Do something awesome with the files..
            }
}

关于asp.net - 如何在 .net web api Controller 中读取多部分表单数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12545788/

相关文章:

ASP.NET MVC 如何在运行时添加新页面?

c# - 我想在 GridView 中编辑我的表格

c# - 如何在列表框上设置多个 SelectedItem?

c# - Web.API : Unable to set Culture

c# - 如何在 .NET 3.5 中为 HttpWebRequest 指定 >2GB 的范围

asp.net-mvc - 如何使输入字段仅允许使用 EF 和数据注释的数字?

特定父目录的 C# 路径

c# - 在已编译查询中重用现有的 linq 表达式

c# - SelectList 中的 ASP.NET MVC 下拉列表

asp.net-mvc - ASP.NET MVC PDFsharp 生成 PDF : cannot add font