3

我创建了一个将 URL(HTML) 的内容转换为图像 (JPG) 的操作,然后将其返回给我,但是,浏览器正在下载它而不是显示。

图像是动态生成的,我没有在硬盘中。

我的代码如下:

[AllowAnonymous]
public FileResult URLToImage(string url)
{
    // In some cases you might want to pull completely different URL that is not related to your application.
    // You can do that by specifying full URL.
    UrlAsImage urlImg = new UrlAsImage(url)
    {
        FileName = Guid.NewGuid().ToString() + ".jpg",
        PageHeight = 630,
        PageWidth = 1200
    };
    return File(urlImg.BuildFile(this.ControllerContext), "image/jpg", urlImg.FileName);
}

我需要做什么才能在浏览器中显示它而不是下载它?

4

1 回答 1

4

返回一个 FileResult 并将标头中的Content-Disposition设置为Inline

[AllowAnonymous]
public FileResult URLToImage(string url)
{
    // In some cases you might want to pull completely different URL that is not related to your application.
    // You can do that by specifying full URL.
    UrlAsImage urlImg = new UrlAsImage(url)
    {
        FileName = Guid.NewGuid().ToString() + ".jpg",
        PageHeight = 630,
        PageWidth = 1200
    };
    Response.AddHeader("Content-Disposition", "inline; filename="+urlImg.FileName);
    return File(urlImg.BuildFile(this.ControllerContext), "image/jpg");
}
于 2015-11-10T08:03:16.833 回答