【问题标题】:MVC Image - Dispaly in a Tab instead of downloading to a fileMVC 图像 - 在选项卡中显示而不是下载到文件
【发布时间】: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


【解决方案1】:

你需要一个 ImageController 来渲染它。

一旦你有了一个控制器,你就可以如下渲染:

public class ImageController{

public ActionResult ShowImage(string path) 
{

    return File(path);
}

}

在你看来:

<img src="@Url.Action("Render","Image", new {id =1  // or path })" />

此答案取自https://stackoverflow.com/a/16142574/5586581

【讨论】:

  • 这个问题与问题无关! (OP 已经返回 FileResult
  • 如果您阅读帖子,主要区别在于显示图像的形式,我表明当返回图像时它可以显示
  • 你认为这会如何显示在另一个标签中!
  • 它取决于工具是否适用于选项卡,例如 Jquery 选项卡,所以 jsfiddle.net/31oftamq/221
  • OP 指的是浏览器标签 :)
猜你喜欢
  • 1970-01-01
  • 2011-03-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-02-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多