1

I have have been developing a web api and in one endpoint in my api, image is uploaded with IFormFile and at this point only acceptable image type is .jpg. To determine the image type I'm using var fileExtension = Path.GetExtension(request.File.FileName).ToLower(); and request.File.ContentType in which request's File is IFormFile.

My problem occurred when I try to upload an file with bare html content (nothing to do with image obviously) with .jpg extension and var fileExtension = Path.GetExtension(request.File.FileName).ToLower(); line returned me ".jpg" without checking the content, also request.File.ContentType returns ".jpg" as well. Is there any built-in support for this in ASP.NET Core 2.1? Any useful suggestion is appreciated.

Edit: This post is suggested to be repost fairly but what I wanna learn is that does .net core handles this as built-in function rather than searching for the magick numbers in the image to validate image type(file extension). Also, it seems that there is no coverage for this by the .net core framework after observing the results empirically.

Hasan
  • 1,243
  • 12
  • 27
  • 2
    There is no magic method that would tell you "correct" file extension for a given file contents. If you have to guess the meaning of the file based on its content, you need to [do it yourself](https://codereview.stackexchange.com/q/85054). – GSerg Dec 25 '18 at 08:20
  • 1
    Possible duplicate of [How to check if a byte array is a valid image?](https://stackoverflow.com/questions/8349693/how-to-check-if-a-byte-array-is-a-valid-image) – Asons Dec 25 '18 at 08:24

0 Answers0