如何在Postman中上传文件到控制器

3

我已经阅读了很多帖子和文章,但仍然不知道为什么我的请求失败了。这是一个asp net core 3.1应用程序。简单的控制器代码:

[Route("api/v1/user")]
public class BlobController : Controller
{
    [HttpPost("uploadphoto")]
    public async Task<UploadPhotoResponse> UploadPhoto([FromForm] IFormFile file)
    {
        return null;
    }
}

enter image description here

Postman请求:

enter image description here

头部信息:

enter image description here

每次我都得到400错误请求的结果,无法进入UploadPhoto方法。


1
400错误请求意味着客户端出现了错误,我已经检查了您的设置,并在Postman中使用了您的Cades和设置,但没有出现错误。您是否使用了任何自定义中间件? - Ruikai Feng
@RuikaiFeng 实际上是的。我使用Zscaler。也许这是个问题。 - Сергей
你可以尝试不用你的自定义中间件,或者在你的自定义中间件中打一个断点,观察你调试时会发生什么。 - Ruikai Feng
2个回答

2

IFormFile更改为UploadPhotoRequest-->多部分文件


using System;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace UploadPhoto.Controllers
{

    public class UploadPhotoRequest
    {
        public IFormFile File { get; set; }
        public string Name { get; set; }

        public string FileName { get; set; }
    }

    [ApiController]
    [Route("api/v1/user")]
    public class WeatherForecastController : ControllerBase
    {


        [HttpPost("uploadphoto")]
        public string UploadPhoto([FromForm] UploadPhotoRequest file)
        {
            Console.WriteLine(file.Name);
            return file.Name;

        }
    }
}

使用Postman集合进行验证

{"auth":null,"event":null,"info":{"_postman_id":null,"description":null,"name":"test.http","schema":"https://schema.getpostman.com/json/collection/v2.1.0/collection.json","version":null},"item":[{"description":null,"event":null,"id":null,"name":"1","protocolProfileBehavior":null,"request":{"auth":null,"body":{"disabled":null,"file":null,"formdata":[{"contentType":"application/json","description":null,"disabled":null,"key":"image","type":"file","value":"settings.json","src":null},{"contentType":null,"description":null,"disabled":null,"key":"name","type":"text","value":"ram","src":null}],"graphql":null,"mode":"formdata","options":null,"raw":null,"urlencoded":null},"certificate":null,"description":"1","header":null,"method":"POST","proxy":null,"url":"https://localhost:5001/api/v1/user/uploadphoto"},"response":null,"variable":null,"auth":null,"item":null}],"protocolProfileBehavior":null,"variable":null}

有两种上传文件的方式:
  1. 通过单个文件上传
  2. 通过多部分上传

如果需要上传多个文件/字段,则使用此选项。

在postman中上传单个文件

  1. 切换到body标签页
  2. 选择二进制
  3. 选择文件 Single Binary

在postman中上传多部分文件

  1. 切换到body标签页
  2. 选择表单数据
  3. 添加键

如果是文件,请将鼠标悬停在键的右侧部分。您将看到将其更改为文件的选项。

Multipart

替代方案

Dothttp 是一款类似的工具,可以更好地控制这些内容。

对于单个文件

POST https://req.dothttp.dev
// select as a binary
fileinput('C:\Users\john\documents\photo.jpg') // path to file

关于分块上传

POST https://req.dothttp.dev
// selects as multipart 
multipart(
    'name'< 'john',
    'photo'< 'C:\Users\john\documents\photo.jpg',
// and many more
)

我已经尝试了这两种方法。其中一种就在我的屏幕上。也许控制器代码不正确? - Сергей
你能分享一下 UploadPhotoResponse 的代码和响应吗? - cedric
你能检查一下最新的修改吗?似乎问题不在Postman上,而是当前代码上。如果你正在使用多部分请求,那么ASP必须知道明确的结构,否则它将会响应“无法处理的实体”或“错误的请求”。 - cedric
你在本地主机上尝试过这个解决方案吗?我尝试了,但仍然失败了。 - Сергей
让我们在聊天中继续这个讨论。点击此处进入聊天室 - cedric
显示剩余2条评论

0
尽管听起来有些奇怪,但请确保在您的控制器中使用"file"作为参数名称,例如。
public async Task<IActionResult> CheckContent(IFormFile file)

只有一种方法可以从Postman发布到MVC控制器 并记得禁用你的防伪方法

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接