How would i go about using MultipartFormDataStreamProvider and Request.Content.ReadAsMultipartAsync in a ApiController?
I have googled a few tutorials but I can’t get any of them to work, I’m using .net 4.5.
Code:
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 anyone know what I am doing wrong or have a working example in asp.net?
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
I resolved the error, i don’t understand what this has to do with end of multipart stream but here is the working code:
public class TestController : ApiController
{
const string StoragePath = @"T:WebApiTest";
public async Task<HttpResponseMessage> 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))
{
return 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.Move(fileData.LocalFileName, Path.Combine(StoragePath, fileName));
}
return Request.CreateResponse(HttpStatusCode.OK);
}
else
{
return Request.CreateResponse(HttpStatusCode.NotAcceptable, "This request is not properly formatted");
}
}
}
Method 2
At first, you should define enctype to multipart/form-data in ajax request header.
[Route("{bulkRequestId:int:min(1)}/Permissions")]
[ResponseType(typeof(IEnumerable<Pair>))]
public async Task<IHttpActionResult> PutCertificatesAsync(int bulkRequestId)
{
if (Request.Content.IsMimeMultipartContent("form-data"))
{
string uploadPath = HttpContext.Current.Server.MapPath("~/uploads");
var streamProvider = new MyStreamProvider(uploadPath);
await Request.Content.ReadAsMultipartAsync(streamProvider);
List<Pair> messages = new List<Pair>();
foreach (var file in streamProvider.FileData)
{
FileInfo fi = new FileInfo(file.LocalFileName);
messages.Add(new Pair(fi.FullName, Guid.NewGuid()));
}
//if (_biz.SetCertificates(bulkRequestId, fileNames))
//{
return Ok(messages);
//}
//return NotFound();
}
return BadRequest();
}
public class MyStreamProvider : MultipartFormDataStreamProvider
{
public MyStreamProvider(string uploadPath) : base(uploadPath)
{
}
public override string GetLocalFileName(HttpContentHeaders headers)
{
string fileName = Guid.NewGuid().ToString() + Path.GetExtension(headers.ContentDisposition.FileName.Replace(""", string.Empty));
return fileName;
}
}
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0