2

I've been using asp.net web api to upload images to azure blob storage. The code controller code looks like this below and which I found searching the web(can't remember where was a while ago). Anyways this works great. However since other files than images can be uploaded to azure I'd like a way to check if the file is a image aswell. I've seen others asking about this but not been able to implement it using the code below.

Question

How to validate if the file is a image using the code below? And if possible what would be best practice/most safe way to validate this? Any help or input appreciated.

EDIT

Updated with the code i tried to implement, not working though

    [HttpPost]
    [Route("api/uploadImage")]
    [ResponseType(typeof(List<BlobUploadModel>))]
    public async Task<IHttpActionResult> PostBlobUpload()
    {
        try
        {
            // This endpoint only supports multipart form data
            if (!Request.Content.IsMimeMultipartContent("form-data"))
            {
                return StatusCode(HttpStatusCode.UnsupportedMediaType);
            }

            //Added this code to convert to Byte and check if it is a image
            Byte[] byteArray = await Request.Content.ReadAsByteArrayAsync();
            bool isvalidImage = IsValidImage(byteArray);

            if (isvalidImage == false)
            {
                return BadRequest();
            }


            // Call service to perform upload, then check result to return as content
            var result = await _service.UploadBlobs(Request.Content);
            if (result != null && result.Count > 0)
            {
                return Ok(result);
            }

            // Otherwise
            return BadRequest();
        }
        catch (Exception ex)
        {
            return InternalServerError(ex);
        }
    }



    //Method that is being called to validate if image
     public static bool IsValidImage(byte[] bytes)
    {
        try
        {
            using (MemoryStream ms = new MemoryStream(bytes))
                Image.FromStream(ms);
        }
        catch (ArgumentException)
        {
            return false;
        }
        return true;
    }
Mattias
  • 2,929
  • 5
  • 29
  • 46

1 Answers1

1

You have to add validation, as suggested in below link to check the ByteArray which is received , otherwise return BadRequest. How to check if a byte array is a valid image?

Community
  • 1
  • 1
Gaurav P
  • 1,097
  • 1
  • 14
  • 19
  • Do you think you can help with how to implement that solution in the code I've added. I'm a beginner and not sure on how to use them together, thanks for the link! – Mattias Aug 10 '16 at 09:18
  • What are you receiving in Request.Content, can you debug and check? – Gaurav P Aug 10 '16 at 09:20
  • I think i found a way to do this, not sure if best practice tho. I added the code Byte[] byteArray = await Request.Content.ReadAsByteArrayAsync(); so that i then can check the byteArray with the code from the link you shared – Mattias Aug 10 '16 at 09:25
  • Regarding your remark on best practice, I would say processing server side have an overhead on bandwidth - I do not see any other issue. – Gaurav P Aug 10 '16 at 09:33
  • @mattias: Please update your code, once you achieve your goal. This will be helpful Thanks – Gaurav P Aug 10 '16 at 09:38
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/120584/discussion-between-mattias-and-gaurav-p). – Mattias Aug 10 '16 at 09:44