0
votes

I'm new in backend development and Gettig 415 Unsupported Media Type in multipart API written in .net core. I have attached the postman image for your reference. Thanks in advance.

[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);
            }

        }
    }

Getting in response on the postman. enter image description here

1
Change your [FromBody] to [FromForm].Qing Guo

1 Answers

1
votes

1.Change your [FromBody] to [FromForm]

2.In the Body tab, select the form-data option. Then hover your mouse over the row so you can see a dropdown appear that says Text. Click this dropdown and set it to File.

enter image description here

enter image description here

Below is a work demo, you can refer to it.

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

Result:

enter image description here