asp.net-mvccontroller-action

want to select image from server path and convert it to binary form


public class AdminController : Controller
   {
       public HHCCEntities hc = new HHCCEntities();
       public ActionResult ServicesView()
    {
        var x = hc.Services.ToList();
        return View(x);
    }
    [Authorize]
    public ActionResult AddServices(int id = 0)
    {

        if (id == 0)
        {
            return View(new Service());
        }
        else
        {
            Service s = new Service();
            HttpResponseMessage response = 
GlobalVariables.webapiclient.GetAsync("Services/" + id.ToString()).Result;
            s = response.Content.ReadAsAsync<Service>().Result;
            return View(s);

        }

    }
     private byte[] GetBinaryFile(string filename)
    {
        byte[] bytes;
        using (FileStream file = new FileStream(filename, FileMode.Open, FileAccess.Read))
        {
            bytes = new byte[file.Length];
            file.Read(bytes, 0, (int)file.Length);
        }
        return bytes;
    }
    [Authorize]
    [HttpPost]
    public ActionResult AddServices(Service s, HttpPostedFileBase ImageFile)
    {
        if (ImageFile != null)
        {
            s.Simage = new byte[ImageFile.ContentLength];
            ImageFile.InputStream.Read(s.Simage, 0, ImageFile.ContentLength);
        }
        if (s.ID==0)
        {
            if (ImageFile == null)
            {

                string filename = "MVC_WEBAPI\\images\\patient.png";

here i want some code that select image from server path and convert it into a binary stream.

The variable filename contain path of the image which is already saved in a server.

This path throws error.

My question is how to get proper right path for image that is stored in images folder in a project?

               byte[] bytes = GetBinaryFile(filename);

            }
            HttpResponseMessage response = GlobalVariables.webapiclient.PostAsJsonAsync("Services", s).Result;
        }
        else
        {
            HttpResponseMessage response = GlobalVariables.webapiclient.PutAsJsonAsync("Services/" + s.ID, s).Result;
        }
        return View();
    }
}

}

This in error


Solution

  •           if (ImageFile == null)
                   {  
                    string filename = "/images/Services.png";
                    var dir = Server.MapPath(filename);
                    byte[] bytes = System.IO.File.ReadAllBytes(dir);
                    s.Simage = bytes;
                   }
        HttpResponseMessage response = 
    GlobalVariables.webapiclient.PostAsJsonAsync("Services", s).Result;