【问题标题】:ASPNetCore API IFormFile returning "invalid input"ASPNetCore API IFormFile 返回“无效输入”
【发布时间】:2019-03-02 13:05:27
【问题描述】:

我有一个 api,我正在尝试上传图片。我尝试使用 swagger & postman。

我得到的只是

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

这就是我的代码的样子 ->

控制器 -

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

图像处理程序 -

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);
    }
}

最后是图片作者

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;
    }
}

我正在学习本教程 -> https://www.codeproject.com/Articles/1256591/Upload-Image-to-NET-Core-2-1-API

我已经在项目中修改了 Swagger 以便能够上传文件,但即使是 swagger 也会抛出同样的错误。 我的代码中没有其他错误,调试也无济于事(尝试在我的控制器中设置断点,但显然他们不接受或不应该接受,仍然在这里学习)。

任何帮助将不胜感激。谢谢!

做了一个干净的测试,问题似乎出在[ApiController]。知道为什么/如何解决它吗?

【问题讨论】:

  • 你在邮递员中选择了form-data和type=file吗?
  • @Baral 当然可以,否则我将无法正确测试它。
  • 发现问题。这显然是因为我的 BlogController 中有 [ApiController]。有什么办法吗?
  • 您使用的是 ASP.NET Core 2.0 还是 2.1?您的标签显示为 2.0,但在 2.1 中添加了 ApiController。无论如何,如果您按照说明here设置兼容版本,我认为您会解决它。
  • @hubert17 查看柯克链接的内容。刚刚发现另一个似乎已经修复的stackoverflow问题。 stackoverflow.com/questions/50919430/… 谢谢大家。

标签: c# asp.net-core-2.0 asp.net-core-webapi


【解决方案1】:

找到了我的问题并找到了我的解决方法。 在发现我的问题是 [ApiController] 后,我偶然发现了另一个与此相关的 stackoverflow 问题。

这解决了我的问题。将services.AddMvc()修改为-> services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);

【讨论】:

    猜你喜欢
    • 2014-07-12
    • 2019-04-01
    • 2021-02-27
    • 1970-01-01
    • 1970-01-01
    • 2022-06-11
    • 2020-08-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多