-1

i am creating web Api for retrieve image from server folder,in c# , i need code for that, thanks in advance,

[HttpPost]
[Route("Fileretrive")]
public void retrive(string filename)
{
    //var ctx = HttpContext.Current;
    var root = HttpContext.Current.Server.MapPath("~/Photos");
    var tempath = Path.Combine(root,filename);
    Directory.GetFiles(tempath);
    byte[] byteArray = null;
    byteArray = System.IO.File.ReadAllBytes(tempath);

}
Prasad Telkikar
  • 15,207
  • 5
  • 21
  • 44

1 Answers1

0

you are nearly there. first you need to return the action result. since you are streaming byte, use file as your return.

[HttpPost]
[Route("Fileretrive")]
public IActionResult retrive(string filename)
{
    //var ctx = HttpContext.Current;
    var root = HttpContext.Current.Server.MapPath("~/Photos");
    var tempath = Path.Combine(root,filename);
    Directory.GetFiles(tempath);
    byte[] byteArray = null;
    byteArray = System.IO.File.ReadAllBytes(tempath);
    // check your mime type, and set your unique file name. maybe use DateTime for your file name
    return File(byteArray , "image/jpeg", $"{DateTime.Now.ToString("ddMMMyyyy")}.jpeg"); 

}
phonemyatt
  • 1,287
  • 17
  • 35