获取 .net 核心不支持的媒体类型

Getting Unsupported Media Type for .net core

我是后端开发和 Gettig 的新手 415 不支持的媒体 输入用 .net 核心编写的多部分 API。我已附上邮递员图片供您参考。提前致谢。

[HttpPost]
    [Route("uploadFiles")]
    public async Task<ActionResult<IEnumerable<status>>> UploadFilesAsync([FromBody] UploadFile uploadFile)
    {

        using (var client = new AmazonS3Client("Access key", "Secret key", Region))
        {
            status s = new status();
            try
            {

                if (uploadFile.Image != null && uploadFile.Image.Length > 0 && uploadFile.Name != null && uploadFile.Name != "")
                {
                    using (var fileStream = new FileStream(uploadFile.Image.FileName, FileMode.Create))
                    {
                        uploadFile.Image.CopyTo(fileStream);

                        var uploadRequest = new TransferUtilityUploadRequest
                        {
                            InputStream = fileStream,
                            Key = uploadFile.Name,
                            BucketName = "needbucket",
                            CannedACL = S3CannedACL.PublicRead

                        };

                        var fileTransferUtility = new TransferUtility(client);
                        await fileTransferUtility.UploadAsync(uploadRequest);

                    }
                    s.Status = "1";
                    s.resone = "File uploded sucesefully.";
                    return Ok(s);
                }
                else
                {
                    s.Status = "0";
                    s.resone = "Image and Image name canot be blank";
                    return Ok(s);
                }

            }
            catch (Exception e)
            {
                s.Status = "0";
                s.resone = "Somthing went wrong." + e.Message;
                return Ok(s);
            }

        }
    }

正在回复邮递员。

1.Change 你的 [FromBody][FromForm]

2.In 正文选项卡,select form-data 选项。然后将鼠标悬停在该行上,这样您就可以看到显示文本的下拉菜单。单击此下拉菜单并将其设置为文件。

下面是工作demo,大家可以参考一下

public class UploadFile
    {
        public string Name { get; set; }       
        public IFormFile Image { get; set; }//this is my key name
       
    }

结果: