For preservation's sake - here's the outline of what Jamie's blog said:
Use a Controller:
Get:
public HttpResponseMessage Get(int id)
{
var result = new HttpResponseMessage(HttpStatusCode.OK);
String filePath = HostingEnvironment.MapPath("~/Images/HT.jpg");
FileStream fileStream = new FileStream(filePath, FileMode.Open);
Image image = Image.FromStream(fileStream);
MemoryStream memoryStream = new MemoryStream();
image.Save(memoryStream, ImageFormat.Jpeg);
result.Content = new ByteArrayContent(memoryStream.ToArray());
result.Content.Headers.ContentType = new MediaTypeHeaderValue("image/jpeg");
return result;
}
Delete:
public void Delete(int id)
{
String filePath = HostingEnvironment.MapPath("~/Images/HT.jpg");
File.Delete(filePath);
}
Post:
public HttpResponseMessage Post()
{
var result = new HttpResponseMessage(HttpStatusCode.OK);
if (Request.Content.IsMimeMultipartContent())
{
//For larger files, this might need to be added:
//Request.Content.LoadIntoBufferAsync().Wait();
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;
String filePath = HostingEnvironment.MapPath("~/Images/");
//Note that the ID is pushed to the request header,
//not the content header:
String[] headerValues = (String[])Request.Headers.GetValues("UniqueId");
String fileName = headerValues[0] + ".jpg";
String fullPath = Path.Combine(filePath, fileName);
image.Save(fullPath);
}
});
return result;
}
else
{
throw new HttpResponseException(Request.CreateResponse(
HttpStatusCode.NotAcceptable,
"This request is not properly formatted"));
}
}