我正在使用带有C#和Azure的Web Api 2,以及如何返回图像(从Memorystream基础)以便在页面上显示的问题...
这是我的Controller HTTPGET
[Route("api/PhotoSubmit/GetPhoto/{id}")]
[HttpGet]
public HttpResponseMessage GetPhotoById(int id)
{
StorageServices storage = new StorageServices();
MemoryStream ms = storage.DownloadBlob(id);
// return what ?
}
Run Code Online (Sandbox Code Playgroud)
这是servicecall的开头:
$http({
method: 'GET',
url: 'api/PhotoSubmit/GetPhoto/' + $routeParams.id,
accept: 'application/json'
})
.success(function(result) {
// How do i handle the result and what HTML should i use ? <img ?
});
Run Code Online (Sandbox Code Playgroud) 我有这个非常简单的DTO:
[DataContract]
public class DTO
{
[DataMember]
public byte[] Image {get; set; }
}
Run Code Online (Sandbox Code Playgroud)
这个非常简单的服务:
[ServiceContract]
public interface IFooService
{
[WebGet(
UriTemplate = "",
RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json)]
List<DTO> GetDTOs();
}
Run Code Online (Sandbox Code Playgroud)
在我的global.asax中,我有:
RouteTable.Routes.Add(new ServiceRoute("foo",
new WebServiceHostFactory(), typeof(FooService)));
Run Code Online (Sandbox Code Playgroud)
现在,当我从浏览器调用它时,我得到一个JSON格式的字节数组.目前很好.现在,如何将该字节数组转换为图像?
或者,有没有更好的方法来解决这个问题?我尝试更改byte[]
为Stream
,但是当我从Firefox调用该服务时,尽管HTTP状态代码为200,但响应为空.我正在使用Firebug和Fiddler.
我不认为这是相关的,但由于太多的信息从来没有伤害任何不是机器人的人,这里是web.config:
<?xml version="1.0"?>
<configuration>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" aspNetCompatibilityEnabled="true" />
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true">
<add name="UrlRoutingModule" type="System.Web.Routing.UrlRoutingModule, System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
</modules>
</system.webServer>
<system.web>
<compilation …
Run Code Online (Sandbox Code Playgroud) 我有一个WebAPI控制器,需要根据请求下载一些文件,但是当涉及到纯文本文件时,它不会给我浏览器的下载请求.它只给出了纯文本响应,就像它是JSON(在我的例子中,它是一个JSONP WebAPI).
我已经从堆栈(和其他网站)检查了其他问答,但我仍然一无所获:
这是我当前的代码:
var httpResponse = new HttpResponseMessage(HttpStatusCode.OK);
httpResponse.Content = new StreamContent(new MemoryStream(Encoding.UTF8.GetBytes(content)));
httpResponse.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
httpResponse.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment");
httpResponse.Content.Headers.ContentDisposition.FileName = "speedcam.txt";
return httpResponse;
Run Code Online (Sandbox Code Playgroud)
这是Chrome的回应:
缓存控制:无缓存
内容处置:附件; 文件名= speedcam.txt
内容长度:17462
内容类型:应用/八位字节流
日期:星期一,2012年8月27日04:53:23 GMT
到期日:-1
附注:无缓存
服务器:Microsoft-IIS/8.0
X-ASPNET-版本:4.0.30319
X-已启动方式:ASP.NET
X-SourceFiles:= UTF-8乙TTpcVHJhYmFsaG9cTWFwYVJhZGFyXE1hcGFSYWRhci5XZWJBUEk0XEV4cG9ydGE =????
似乎没问题,但整个文件文本都在Chrome开发工具"响应选项卡"中.
我一直在尝试使用 ASP.NET Web API 返回图像。我见过很多例子,但我不断遇到问题。
\n\n在搜索解决方案后,大多数示例建议使用 HttpResponseMessage 并正确设置 Content 和 Content-Type 标头。例如,在以下帖子中:\n WebApi:如何处理图像 ASP .Net Web API 将图像下载为二进制文件
\n\n这就是我现在正在做的事情:
\n\n [System.Web.Http.HttpGet]\n [ActionName("ConvertHTMLToImage")]\n public HttpResponseMessage ConvertHTMLToImage(string htmlString)\n {\n var path = @"C:\\temp\\mona-lisa.png";\n var response = Request.CreateResponse(HttpStatusCode.OK);\n var fileStream = new FileStream(path, FileMode.Open, FileAccess.Read);\n response.Content = new StreamContent(fileStream);\n response.Content.Headers.ContentType = new MediaTypeHeaderValue("image/png");\n return response;\n }\n
Run Code Online (Sandbox Code Playgroud)\n\n我得到的结果好坏参半。当我访问该资源的 URL 时,我遇到了 Chrome(此网页不可用)、Firefox(连接已重置)和 Safari(Safari 无法打开该页面)的问题。令我惊讶的是,它在 IE 中运行得很好。我还可以使用 Fiddler 中的作曲家成功查看图像。如果我保持 Fiddler 打开并使用 Chrome/Firefox/Safari 访问资源,我会看到 504 错误。不确定这意味着什么,我不记得以前见过该错误类型。
\n\n我还注意到调试器中的浏览器无法正常工作时出现一些奇怪的行为。如果我从 IE 和 Fiddler 调用 ConvertHTMLToImage,我会看到它在断点处停止一次,然后图像成功返回给客户端。在 Chrome/Firefox/Safari …