如何将文件从邮递员上传到控制器
How to upload file from postman to controller
我已经阅读了很多 post 和文章,但仍然不知道为什么我的请求失败了。有一个 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;
}
}
邮递员请求:
Headers :
每次我收到 400 错误请求 结果并且无法进入 UploadPhoto 方法。
将 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;
}
}
}
邮递员合集验证
{"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}
上传文件有两种方式。
- 通过单个文件上传
- 通过多部分上传
This option is used, if there are multiple files/fields to upload
要上传单个文件,在 postman 中
- 切换到正文标签
- select二进制
- select 文件
要上传多部分文件,在 postman 中
- 切换到正文标签
- select form-data
- 添加键
if it is a file, hover to the right section of key. you will see option to change it to file.
备选方案
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
)
我已经阅读了很多 post 和文章,但仍然不知道为什么我的请求失败了。有一个 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;
}
}
邮递员请求:
Headers :
每次我收到 400 错误请求 结果并且无法进入 UploadPhoto 方法。
将 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;
}
}
}
邮递员合集验证
{"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}
上传文件有两种方式。
- 通过单个文件上传
- 通过多部分上传
This option is used, if there are multiple files/fields to upload
要上传单个文件,在 postman 中
- 切换到正文标签
- select二进制
- select 文件
要上传多部分文件,在 postman 中
- 切换到正文标签
- select form-data
- 添加键
if it is a file, hover to the right section of key. you will see option to change it to file.
备选方案
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
)