Display screen image from MVC

I have images in a database and I want to return an image for viewing from an action. Here is my action.

public FileContentResult Index(ItemImageRequest request) { var result = queueService.GetItemImage(request); if (result.TotalResults == 0) return File(new byte[0], "image/jpeg"); var image = result.FirstResult; return File(image.Image, "image/tif"); } 

I also tried this code

 public FileStreamResult Index(ItemImageRequest request) { //retrieval omitted var image = result.FirstResult; System.IO.Stream stream = new System.IO.MemoryStream(image.Image); return new FileStreamResult(stream, "image/tif"); } 

When I go to my action in the browser, it offers me a download. I do not want it to load, I want it to be displayed in the browser. How to do it?

+4
source share
3 answers

If you use return Controller.File(filename, ...) , you will return a FilePathResult, which is probably not the one you want - I assume that the "image" in your sample is the name of the image file (case when " var "isn 't helping someone ...)

If you use one of the other file overloads or directly use FileContentResult or FileStreamResult, you will get the desired effect.

You do not need to create your own ActionResult class (although, of course, this may be useful for other reasons.)

HOWEVER, having just written all this, I realized that your problem is that TIFF is not a file format that browsers can always (ever?) Display inside, which is probably the reason that they suggest downloading .

You will need to re-display it in PNG or something on the server to display in the browser.

+8
source

File ActionResult adds a line to the HTTP header as follows:

 Content-disposition: attachment; filename=foo 

This will force the browser to try to download the file. This is why there is an overload for specifying a file name.

You can create your own ActionResult to load and omit Content-disposition.

If you want to copy and paste, I have code for one on codeplex: ImageResult.cs

+5
source

As requested, here is my solution.

Here is the ImageResult class copied and modified from John

 public class ImageResult : ActionResult { public ImageResult() { } public byte[] ImageData { get; set; } public MemoryStream ImageStream { get; set; } public string MimeType { get; set; } public HttpCacheability Cacheability { get; set; } public string ETag { get; set; } public DateTime? Expires { get; set; } public override void ExecuteResult(ControllerContext context) { if (this.ImageData == null && ImageStream == null) { throw new ArgumentNullException("ImageData or ImageStream"); } if (string.IsNullOrEmpty(this.MimeType)) { throw new ArgumentNullException("MimeType"); } context.HttpContext.Response.ContentType = this.MimeType; if (!string.IsNullOrEmpty(this.ETag)) { context.HttpContext.Response.Cache.SetETag(this.ETag); } if (this.Expires.HasValue) { context.HttpContext.Response.Cache.SetCacheability(this.Cacheability); context.HttpContext.Response.Cache.SetExpires(this.Expires.Value); } if (ImageStream != null) { ImageData = ImageStream.ToArray(); } context.HttpContext.Response.OutputStream.Write(this.ImageData, 0, this.ImageData.Length); } } 

Here is my action, modified for clarity

 public ActionResult Index(ItemImageRequest request) { var result = queueService.GetItemImage(request); if (result.TotalResults == 0) return new EmptyResult(); ItemImage image = result.FirstResult; //image.Image is type byte[] MemoryStream tiffStream = new MemoryStream(image.Image); MemoryStream pngStream = new MemoryStream(); System.Drawing.Bitmap.FromStream(tiffStream).Save(pngStream, System.Drawing.Imaging.ImageFormat.Png); return new ImageResult() { ImageStream = pngStream, MimeType = "image/png", Cacheability = HttpCacheability.NoCache }; } 

Thanks to John and Will for helping me with this.

+1
source

Source: https://habr.com/ru/post/1342042/


All Articles