Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to return an image from Asp.Net MVC Controller

    • 0
    • 0
    • 0
    • 1
    • 0
    • 0
    • 0
    • 3.55k
    Answer it

    Hi,
         While working in a Asp.Net MVC project, I am stuck in a issue that how to return an image from MVC Controller ?

    The real senario is that I have to return an image among various images (that I have kept in the Content folder in the Solution Explorer) as per the input from the user. Please tell me how can I achieve this if anyone has any idea as I am new to Asp.Net MVC.

    What I am doing is as follows:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    
    namespace RoutePOC.Controllers
    {
        public class HomeController : Controller
        {
    
            public ActionResult Index()
            {
    
               return View();            
             }
    
              [HttpPost]        
            public ActionResult Search(int choice)
            {
               if(choice==1)
                 //return image1.... How to do this?
    
            }
    
    
       }
    }
    

    Thanks in advance..!

 1 Answer(s)

  • Hi Mayank, This can be done by following code

    return File("~/Content/Penguins.jpg","image/jpg");
    

    That means you are telling to return the Penguins.jpg file from the content folder(The path may be dynamic)
    and the second parameter tells that the file type returned should be of image type

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: