asp.net web api 上传多个文件

if(Request.Content.IsMimeMultipartContent())

{

var streamProvider =newMultipartMemoryStreamProvider();

var task =Request.Content.ReadAsMultipartAsync(streamProvider).ContinueWith(t =>//上传多个文件

{

foreach(var item in streamProvider.Contents)

{

//do something

}

});

public class UploadFileController : ApiController

{

public async Task<string> Post()

{

// 检查是否是 multipart/form-data

if (!Request.Content.IsMimeMultipartContent("form-data"))

throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);

// 设置上传目录

var provider = new MultipartFormDataStreamProvider(@"c:\\temp");

// 接收数据,并保存文件

var bodyparts = await Request.Content.ReadAsMultipartAsync(provider);

string result = "";

// 获取表单数据

result += "formData txtName: " + bodyparts.FormData["txtName"];

result += "<br />";

// 获取文件数据

result += "fileData headers: " + bodyparts.FileData[0].Headers; // 上传文件相关的头信息

result += "<br />";

result += "fileData localFileName: " + bodyparts.FileData[0].LocalFileName; // 文件在服务端的保存地址,需要的话自行 rename 或 move

return result;

}

}