Jak obsługiwać obrazy za pomocą WebAPI
Pytania
Jakie są różne sposoby publikowania / przesyłania zdjęć do mojej usługi? Myślę, że mogę albo użyć tekstu Base-64 W JSON, albo pozostać natywnym jako binarny. Rozumiem, że konwertując obraz na tekst, następuje znaczny wzrost rozmiaru pakietu.
Jeśli wysyłam obraz (z formularza internetowego, z natywnego klienta, z innej usługi), Czy powinienem dodać kontroler obrazu/obsługę lub użyć Formatera? Czy to w ogóle jest albo/albo pytanie?
Zbadałem i znalazłem wiele konkurencyjnych przykładów, ale nie jestem pewien, w którym kierunku powinienem zmierzać.
Czy istnieje artykuł na stronie / blogu, który określa plusy i minusy tego?
2 answers
Poszperałem trochę i możecie zobaczyć implementację, którą wymyśliłem tutaj: http://jamessdixon.wordpress.com/2013/10/01/handling-images-in-webapi/
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-10-01 09:54:06
Ze względu na zachowanie-oto zarys tego, co powiedział blog Jamiego:
Użyj kontrolera:
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;
}
Usuń:
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"));
}
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-09-13 07:03:01