簡體   English   中英

如何將文件從郵遞員上傳到控制器

[英]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}

上傳文件有兩種方式。

  1. 通過單個文件上傳
  2. 通過多部分上傳

如果要上傳多個文件/字段,則使用此選項

上傳單個文件,在郵遞員中

  1. 切換到正文選項卡
  2. 選擇二進制
  3. 選擇文件單二進制

上傳多部分文件,在郵遞員中

  1. 切換到正文選項卡
  2. 選擇表單數據
  3. 添加鍵

如果是文件,將鼠標懸停在鍵的右側部分。 您將看到將其更改為文件的選項。

多部分

替代解決方案

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM