The below code works very well on development environment VS 2012, the images are saved to App_Data.
When I deployed the same on Win 8.1, IIS 8.5, the App_Data was missing. I manually create the folder and set the properties as given in http://hintdesk.com/tutorial-publish-asp-net-web-api-in-iis-8-5-and-windows-8-1/.
I am seeing strange behavior as the upload is not consistent. Now and then the upload fails. Not sure why.
The application is hosted on same machine in which the development is done and the port # on IIS is set to same development port#
[HttpPost]
[Route("v1/persons/{personId}/image")]
public IHttpActionResult CreatePersonImage(int personId)
{
if (Request.Content.IsMimeMultipartContent())
{
var isUpdated = 0;
Request.Content.ReadAsMultipartAsync<MultipartMemoryStreamProvider>(new MultipartMemoryStreamProvider()).ContinueWith((task) =>
{
MultipartMemoryStreamProvider provider = task.Result;
foreach (HttpContent content in provider.Contents)
{
Stream stream = content.ReadAsStreamAsync().Result;
Image image = Image.FromStream(stream);
//var testName = content.Headers.ContentDisposition.Name;
var imageFileFullName = Utils.GetImageFileFullName(personId + ".jpg");
image.Save(imageFileFullName);
PersonDb persondb = new PersonDb();
isUpdated = persondb.CreatePersonImage(new Person()
{
ImageFileName = Path.GetFileName(imageFileFullName),
PersonId = personId
}, out InternalServerErr);
}
});
return Ok();
}
else
{
return BadRequest("Error in Content-type. It should be multipart/form-data");
}
}
public static string GetImageFileFullName(string imageFileName)
{
return Path.Combine(HostingEnvironment.MapPath("~/App_Data"), imageFileName);
}
Web config on IIS 8.5
<?xml version="1.0" encoding="utf-8"?>
<!--
For more information on how to configure your ASP.NET application, please
visit
http://go.microsoft.com/fwlink/?LinkId=301879
-->
<configuration>
<appSettings>
</appSettings>
<system.web>
<compilation targetFramework="4.5">
<assemblies>
<add assembly="System.Net.Http, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
</assemblies>
</compilation>
<httpRuntime targetFramework="4.5" />
</system.web>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true" />
<handlers>
<remove name="ExtensionlessUrlHandler-Integrated-4.0" />
<remove name="OPTIONSVerbHandler" />
<remove name="TRACEVerbHandler" />
<add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
</handlers>
</system.webServer>
<connectionStrings>
<add name="Conn" connectionString="Server=localhost;Database=sampledb;User Id=sa; Password=test123"></add>
</connectionStrings>
</configuration>
The application always returns the 200 OK status but I don't see the file on App_Data on IIS.
W3C Log
#Fields: date time s-ip cs-method cs-uri7-stem cs-uri-query s-port cs-username c-ip cs(User-Agent) cs(Referer) sc-status sc-substatus sc-win32-status sc-bytes cs-bytes time-taken CutomType CotentLength
2015-06-10 10:27:52 192.168.10.22 POST /v1/persons/4/image - 50737 - 192.168.10.22 RestSharp/105.1.0.0 - 200 0 0 210 6198 95 multipart/form-data;+boundary=-----------------------------28947758029299 5830
The size of the files are in few KBs
Edit: The problem seems to be with async so I followed https://stackoverflow.com/a/20356591/2922388 to upload synchronously and it worked fine.