[英]How to upload file from postman to controller
我一直在閱讀很多帖子和文章,但仍然不知道為什么我的請求失敗了。 有一個 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;
}
}
郵遞員要求:
標題:
每次我收到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}
上傳文件有兩種方式。
如果要上傳多個文件/字段,則使用此選項
如果是文件,將鼠標懸停在鍵的右側部分。 您將看到將其更改為文件的選項。
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
)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.