Cannot upload a file using ASP.NET Core REST Web API and Swagger

William Johnston 106 Reputation points
2024-10-18T13:30:16.2766667+00:00

Hello,

The [FromForm] IFormFile file is null for a REST API HttpPost.

The project was created using the Visual Studio 2022 ASP.NET Core Web API template.

I deleted the Weather model and controller. And added my own controller.

Here is my controller code:

using Microsoft.AspNetCore.Mvc;
using DocumentChunker;

namespace DocumentChunkerRestAPI.Controllers
{
    public class ChunkerController : Controller
    {
        public IActionResult Index()
        {
            return View();
        }

        [HttpPost("chunk")]

        public IActionResult Chunk([FromForm] IFormFile file, [FromForm] int iParseType)
        {
            if (file == null || file.Length == 0)
            {
                return BadRequest("File not provided");
            }

            if (iParseType < 0 || iParseType > 2)
            {
                return BadRequest("Parse types are from 0 - 2");
            }

            var json = string.Empty;

            // Process the file and the integer as needed
            using (var memoryStream = new MemoryStream())
            {
                file.CopyTo(memoryStream);

                ParseType parseType = ParseType.SENTENCE;

                if (iParseType == 0)
                {
                    parseType = ParseType.SENTENCE;
                }
                else if (iParseType == 1)
                {
                    parseType = ParseType.PARAGRAPH;
                }
                else if (iParseType == 2)
                {
                    parseType = ParseType.PAGE;
                }

                json = new DocumentChunker.Chunker().Chunk(memoryStream, parseType, file.FileName);
            }

            return new JsonResult(json)
            {
                ContentType = "application/json",
                StatusCode = 200
            };
        }

    }
}

Any suggestions?

williamj

Developer technologies ASP.NET ASP.NET Core
{count} votes

3 answers

Sort by: Most helpful
  1. SurferOnWww 4,631 Reputation points
    2024-10-19T02:44:27.15+00:00

    The project was created using the Visual Studio 2022 ASP.NET Core Web API template.

    Why do you use the "Web API template"? The ChunkerController in your code is a controller for MVC application.

    The [FromForm] IFormFile file is null for a REST API HttpPost.

    Use fiddler or equivalent tool to capture the request / response as shown below and check if file is properly sent to server in multipart/form-data format and header includes name="file" attribute which is corresponding to the argument name of action method Chunk([FromForm] IFormFile file, [FromForm] int iParseType).

    enter image description here


  2. Mokhtar Brada 0 Reputation points
    2024-10-19T17:21:26.9+00:00

    good job thank you

    0 comments No comments

  3. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.


    Comments have been turned off. Learn more

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.