Reputation: 1740
With the below code I keep getting the error: Cannot implicitly convert type 'System.Net.Http.MultipartFormDataStreamProvider' to 'System.Threading.Tasks.Task>'
MultipartFormDataStreamProvider streamProvider = new MultipartFormDataStreamProvider("c:\\tmp\\uploads");
//Error line
Task<IEnumerable<HttpContent>> bodyparts = await Request.Content.ReadAsMultipartAsync(streamProvider);
I think this is an easy one, but I'm missing it.
Upvotes: 2
Views: 11843
Reputation: 1740
I ended up using this and it works great. I found it here
http://www.asp.net/web-api/overview/working-with-http/sending-html-form-data,-part-2
using System.Diagnostics;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using System.Web;
using System.Web.Http;
public class UploadController : ApiController
{
public async Task<HttpResponseMessage> PostFormData()
{
// Check if the request contains multipart/form-data.
if (!Request.Content.IsMimeMultipartContent())
{
throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
}
string root = HttpContext.Current.Server.MapPath("~/App_Data");
var provider = new MultipartFormDataStreamProvider(root);
try
{
// Read the form data.
await Request.Content.ReadAsMultipartAsync(provider);
// This illustrates how to get the file names.
foreach (MultipartFileData file in provider.FileData)
{
Trace.WriteLine(file.Headers.ContentDisposition.FileName);
Trace.WriteLine("Server file path: " + file.LocalFileName);
}
return Request.CreateResponse(HttpStatusCode.OK);
}
catch (System.Exception e)
{
return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, e);
}
}
}
Upvotes: 7