下载动态生成的图像而不是在浏览器中显示
Download an image generated dynamically rather than display in browser
我创建了一个将 URL(HTML) 的内容转换为图像 (JPG) 的 Action,然后 return 它给我,但是,浏览器正在下载而不是显示。
图片是动态生成的,我的硬盘里没有。
我的代码如下:
[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);
}
我需要做什么才能在浏览器中显示它而不是下载它?
Return 一个 FileResult 并将 header 中的 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");
}
我创建了一个将 URL(HTML) 的内容转换为图像 (JPG) 的 Action,然后 return 它给我,但是,浏览器正在下载而不是显示。
图片是动态生成的,我的硬盘里没有。
我的代码如下:
[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);
}
我需要做什么才能在浏览器中显示它而不是下载它?
Return 一个 FileResult 并将 header 中的 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");
}