【发布时间】:2018-10-13 09:19:37
【问题描述】:
我有允许用户上传图片的 ASP.NET WEB API 项目,然后在另一个页面中查看这些图片。
图片上传成功;但是,当从移动应用程序调用 GET 方法时,其中一些图像会返回 SERVER ERROR 500。
如果我复制返回错误的 url 并执行来自浏览器或邮递员的请求,则 url 可以正常工作,因此可以在服务器中找到图像。
该网址仅在移动设备上不起作用的原因可能是什么?
这是返回图像的代码:
[HttpGet]
[Route("Stores/{id}/Photos/{pid}")]
public HttpResponseMessage GetStorePhoto(int id, int pid)
{
using (var db = new ApplicationDbContext())
{
var photo = store.Photos.FirstOrDefault(p => p.Id == pid);
if (photo == null)
return ImageNotFound(); //returns a default image in case image not found
var filePath =
HttpContext.Current.Server.MapPath("~/Uploads/Stores/" + photo.FileName);
if (!File.Exists(filePath))
return ImageNotFound();
var response = new HttpResponseMessage();
response.Content = new StreamContent(new FileStream(filePath, FileMode.Open));
response.Content.Headers.ContentType = new MediaTypeHeaderValue("image/png");
return response;
}
}
【问题讨论】:
-
你的服务器日志说什么?在大多数情况下,错误将在那里打印出来。
-
错误只是说'500 Internal Server Error'
-
您能告诉我们您负责返回图像的代码吗?
-
请检查我更新的问题
-
您是否尝试过使用 Visual Studio 的 断点 来验证输出?
标签: asp.net azure asp.net-web-api mobile