I need your helps. I'm making cordova project with VS 2015 Community. My problem:
<form id="formplace" enctype="multipart/form-data" method="post" action="http://localhost:57754/api/app/PostFile">
<input type="text" id="title" name="title" >
<input id="sesid" type="hidden" name="ID" />
<textarea type="text" id="content" name="content" ></textarea>
<input type="file" id="fileUpload" name="fileUpload" accept="image/*" />
<input type="submit" value="Send" />
I can send form values to webapi use form's action. Image upload is Ok. But then I can't use return value from server. I want to send this value use javascript. I tried formdata but I couldn't.
My C# codes :
public async Task<HttpResponseMessage> PostFile()
{
if (!Request.Content.IsMimeMultipartContent("form-data"))
{
throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
}
try
{
var uploadFolder = HostingEnvironment.MapPath("/Content/Upload/");
Directory.CreateDirectory(uploadFolder);
var streamProvider = new MyFormDataStreamProvider(uploadFolder);
await Request.Content.ReadAsMultipartAsync(streamProvider);
foreach (var key in streamProvider.FormData.AllKeys)
{
foreach (var val in streamProvider.FormData.GetValues(key))
{
if (key == "title")
{
title = val;
}
else if (key == "content")
{
content = val;
}
else if (key == "ID")
{
ide = val;
}
}
}
News n = new News() { };
n.ID = ide;
n.Photo = "Content/Upload/"+ dateklasor +"/"+ large1;
n.PhotoSlider = "Content/Upload/" + dateklasor + "/" + medium1;
n.PhotoThumb = "Content/Upload/" + dateklasor + "/" + thumbnail1;
n.Title = title;
n.Content = content;
NewPlace(n);
var response = new HttpResponseMessage();
response.Content = new StringContent("Success");
return response;
}
catch (System.Exception e)
{
var response = new HttpResponseMessage();
response.Content = new StringContent("Trouble");
return response;
}
}
I'm sending with ajax but server side throw excepiton Message = "Unexpected end of MIME multipart stream. MIME multipart message is not complete." – What should I do? Thanks for helps.