'Image sent from android to .net rest api is null on the server

I'm building this android app where I need to send images to the server. I'm using retrofit to do so. I get image path as its answer in this stack overflow question Get file path of image on Android and send images as shown here How to upload an image file in Retrofit 2. When I decode a file that I create in the android studio it is valid, but when I send it to my .net rest API it is null. Here is the rest API code, the idea is to get an image if it's null send an image that says it's null, and if not save that image and send it back. The part where I'm sending it back works perfectly, that is I receive an image sent to android, but the image that is sent from android is null.

        [Route("post")]
        public async Task<IActionResult> PostSlika(IFormFile formFile)
        {


            if (formFile is null)
            {
                var stream2 = new FileStream(Path.Combine(_host.WebRootPath, "Images/null_je.jpg"), FileMode.Open);
                return File(stream2, "image/jpg");
            }
            using (var stream = System.IO.File.Create(Path.Combine(_host.WebRootPath, "Images/1.jpg")))
            {
                await formFile.CopyToAsync(stream);
            }

            var stream1 = new FileStream(Path.Combine(_host.WebRootPath, "Images/1.jpg"), FileMode.Open);
            return File(stream1, "image/jpg");
        }
    }


Solution 1:[1]

I guess there is a conversion problem when converting data to IFormFile. But first check the incomming data with Base64String. I mean:

[Route("post")]
public async Task<IActionResult> PostSlika(string formFileStr)
{
    //Convert formFileStr from Base64String to IFormFile 
}

Note: you should send Base64String to your service to test it.

If that is OK but you don't want to use this approach then you can use UriTemplate like this:

[WebInvoke( Method= "POST",
//These two parameters are not necessary, but you can use them:
RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json,
//
UriTemplate = "PostSlika/?q={formFile}")]
public async Task<IActionResult> PostSlika(IFormFile formFile)
{
    //your codes here
}

Then address to your service method should be changed a little:

{Your Service Address}/PostSlika/?q={object of IFormFile}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Mostafa Khodakarami