Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
847 views
in Technique[技术] by (71.8m points)

asp.net - how to read multi part form data in .net web api controller

public class Sampleontroller:apicontroller
 {    
    public void PostBodyMethod() {
        HttpRequestMessage request=this.request;
     //How to read the multi part data in the method
    }
}

I am sending a multi part data to webapi controller. How to read the contents in the method?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

An 'async' example:

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..
            }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...