0
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

CodeNotFound
  • 22,153
  • 10
  • 68
  • 69
  • What is your specific question? Consider using a question mark w/ your specific question. – JDOaktown May 02 '18 at 00:57
  • 2
    Your question still unclear, if you getting error please explain which error being thrown and in which line the exception occurred. I see that you want to use virtual path to PNG image, hence you may need `Server.MapPath` when retrieve file from server. – Tetsuya Yamamoto May 02 '18 at 01:22
  • Please Check Now – Usman Kiani May 02 '18 at 05:35

2 Answers2

0

You can write your own function:

private byte[] GetBinaryFile(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;
}

Then you can call that function from your controller action:

byte[] bytes = GetBinaryFile("filename.bin");

You could also throw it to Stream:

Stream stream = new MemoryStream(bytes);

EDIT

I am not sure why you get error but there is a shorter version of reading file into binary data. Can you try this one?

byte[] bytes = System.IO.File.ReadAllBytes("Stuff\\file.exe");
gardarvalur
  • 1,565
  • 9
  • 39
  • 65
0
          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;