【发布时间】:2018-02-14 18:52:13
【问题描述】:
我有以下从 web api 获取 PNG 的控制器方法。
public async Task<ActionResult> RealTimeUpdate(string fundName)
{
string docPath = ConfigurationManager.AppSettings["RealTimeUpdate"].Replace("{fundname}",fundName).ToString();
docPath = docPath.Replace("\\\\", "\\");
docPath = docPath.Replace("\"", "");
string url = ServiceUrl + "api/RealTime/" + fundName;
HttpResponseMessage response = await client.GetAsync(url);
if (response.IsSuccessStatusCode)
{
var dataStream = response.Content.ReadAsStringAsync().Result;
if (dataStream == null)
return HttpNotFound();
var _buffer = JsonConvert.DeserializeAnonymousType(dataStream, new { _buffer = (byte[])null })._buffer;
// If user decides to save the file, this will help...
//Response.AddHeader("content-disposition", "filename=" + Path.GetFileName(path));
return File(_buffer, "application/png");
}
return View("Error");
}
我这样称呼它:
<a href="@Url.Action("RealTimeUpdate", "Documents", new { FundName = "RealTimeUpdate"})" class="btn rt-info rtBtn" target="_blank">Real Time Update</a>
如您所见,我有 target="_blank",但是,它不会在新选项卡中显示图像,而是将其下载到我的文档文件夹中。如何让它显示在选项卡中?
【问题讨论】:
-
是的,可以。谢谢。
-
很好,我将答案设置在贡献社区的答案框中
-
您无法控制它,因为它是浏览器设置。但另一种选择是使用
FileStreamResult- 请参阅 Shyju 对this question的回答
标签: asp.net-mvc razor png filestream