How would i go about using MultipartFormDataStreamProvider
and Request.Content.ReadAsMultipartAsync
in a ApiController
?
I have googled a few tutorial but i can't get any of them to work, im using .net 4.5.
This is what i currently got:
public class TestController : ApiController
{
const string StoragePath = @"T:WebApiTest";
public async void Post()
{
if (Request.Content.IsMimeMultipartContent())
{
var streamProvider = new MultipartFormDataStreamProvider(Path.Combine(StoragePath, "Upload"));
await Request.Content.ReadAsMultipartAsync(streamProvider);
foreach (MultipartFileData fileData in streamProvider.FileData)
{
if (string.IsNullOrEmpty(fileData.Headers.ContentDisposition.FileName))
{
throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotAcceptable, "This request is not properly formatted"));
}
string fileName = fileData.Headers.ContentDisposition.FileName;
if (fileName.StartsWith(""") && fileName.EndsWith("""))
{
fileName = fileName.Trim('"');
}
if (fileName.Contains(@"/") || fileName.Contains(@""))
{
fileName = Path.GetFileName(fileName);
}
File.Copy(fileData.LocalFileName, Path.Combine(StoragePath, fileName));
}
}
else
{
throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotAcceptable, "This request is not properly formatted"));
}
}
}
I get the exception
Unexpected end of MIME multipart stream. MIME multipart message is not
complete.
when the await task;
runs.
Does any one have any idea what i am doing wrong or have a working example in a normal asp.net project using web api.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…