问题
我研究并发现了许多相互竞争的例子,但我不确定我应该朝哪个方向发展。
有没有一篇网站/博客文章列出了这种做法的利弊?
发布于 2013-10-01 17:54:07
我做了一些研究,您可以看到我在这里提出的实现:http://jamessdixon.wordpress.com/2013/10/01/handling-images-in-webapi/
发布于 2016-09-13 15:03:01
为了保护--以下是Jamie博客的提纲:
使用控制器:
获取:
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;
}
删除:
public void Delete(int id)
{
String filePath = HostingEnvironment.MapPath("~/Images/HT.jpg");
File.Delete(filePath);
}
帖子:
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"));
}
}
https://stackoverflow.com/questions/19005991
复制相似问题