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

William Johnston 61 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

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,571 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. SurferOnWww 2,906 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

    0 comments No comments

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.