1

I have an api and i'm trying to upload an image. I tried using swagger & postman.

All i'm getting back is

{
    "": [
        "The input was not valid."
    ]
}

This is how my code looks like ->

Controller -

    [HttpPost("images")]
    public async Task<IActionResult> UploadImage(IFormFile file)
    {
        return await _imageHandler.UploadImage(file);
    }

Image Handler -

public interface IImageHandler
{
    Task<IActionResult> UploadImage(IFormFile file);
}

public class ImageHandler : IImageHandler
{
    private readonly IImageWriter _imageWriter;
    public ImageHandler(IImageWriter imageWriter)
    {
        _imageWriter = imageWriter;
    }

    public async Task<IActionResult> UploadImage(IFormFile file)
    {
        var result = await _imageWriter.UploadImage(file);
        return new ObjectResult(result);
    }
}

And finally image writer

public class WriterHelper
{
    public enum ImageFormat
    {
        bmp,
        jpeg,
        gif,
        tiff,
        png,
        unknown
    }

    public static ImageFormat GetImageFormat(byte[] bytes)
    {
        var bmp = Encoding.ASCII.GetBytes("BM");     // BMP
        var gif = Encoding.ASCII.GetBytes("GIF");    // GIF
        var png = new byte[] { 137, 80, 78, 71 };              // PNG
        var tiff = new byte[] { 73, 73, 42 };                  // TIFF
        var tiff2 = new byte[] { 77, 77, 42 };                 // TIFF
        var jpeg = new byte[] { 255, 216, 255, 224 };          // jpeg
        var jpeg2 = new byte[] { 255, 216, 255, 225 };         // jpeg canon

        if (bmp.SequenceEqual(bytes.Take(bmp.Length)))
            return ImageFormat.bmp;

        if (gif.SequenceEqual(bytes.Take(gif.Length)))
            return ImageFormat.gif;

        if (png.SequenceEqual(bytes.Take(png.Length)))
            return ImageFormat.png;

        if (tiff.SequenceEqual(bytes.Take(tiff.Length)))
            return ImageFormat.tiff;

        if (tiff2.SequenceEqual(bytes.Take(tiff2.Length)))
            return ImageFormat.tiff;

        if (jpeg.SequenceEqual(bytes.Take(jpeg.Length)))
            return ImageFormat.jpeg;

        if (jpeg2.SequenceEqual(bytes.Take(jpeg2.Length)))
            return ImageFormat.jpeg;

        return ImageFormat.unknown;
    }
}

I was following this tutorial -> https://www.codeproject.com/Articles/1256591/Upload-Image-to-NET-Core-2-1-API

I've already modified Swagger in the project to be able to upload files, but even swagger throws the same error. I got no other errors in my code & debugging does nothing (trying to set breakpoints in my controller, but apparently they dont take or they aren't supposed to, still learning here).

Any help will be appreciated. Thanks!

enter image description here

Did a clean test and the problem seems to be in [ApiController]. Any idea why/how i can fix it?

17
  • Did you choose form-data in postman and type=file? Commented Sep 26, 2018 at 17:25
  • @Baral Yes of course, otherwise i wouldnt have been able to test it properly. Commented Sep 26, 2018 at 17:26
  • 1
    Found the problem. Its apparently because of [ApiController] that i have in my BlogController. Any way around it ? Commented Sep 27, 2018 at 20:26
  • 1
    Are you using ASP.NET Core 2.0 or 2.1? Your tag says 2.0 but ApiController was added in 2.1. Anyway, I think you'll fix it if you follow the instructions here for setting the compatibility version. Commented Sep 27, 2018 at 20:31
  • 1
    @hubert17 See what Kirk linked. Just found another stackoverflow question that seems to have fixed it. stackoverflow.com/questions/50919430/… Thanks guys. Commented Sep 27, 2018 at 20:35

1 Answer 1

3

Found my problem and found my fix. After finding that my problem was [ApiController] i stumbled upon another stackoverflow problem related to this.

This fixed my problem. Modifyingservices.AddMvc() to -> services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.