-1

I have created one web api in C# where one action method takes object as parameter which includes IFormFile and sends the file for printing. But every time I am calling this function using some c# console app then I am getting this IFormFile as null.

Here is my code for API:

    [Route("print")]
    [HttpPost]
    public IHttpActionResult Print([FromBody] PrintFile printDocument)
    {
        try
        {
            if (ModelState.IsValid)
            {

                var response = _printingService.PrintFile(printDocument.Document, printDocument.PrinterName, printDocument.ServerName);

                 return Ok(response);

             }
             else
             {
                 return Unauthorized();
             }

        }
        catch (Exception ex)
        {
            return BadRequest(string.Format(ErrorMessages.GeneralErrorMsg, ex.Message));
        }
    }

Model class:

public class PrintFile
{
    public string PrinterName { get; set; }

    public string ServerName { get; set; }

    [Required]
    public IFormFile File { get; set; }

    public IDictionary<string, string> Parameters { get; set; }
}

C# client application calling this API method:

    public ResponseData PrintFile(PrintFileInfo printFileInfo)
    {
        try
        {
            _request = new RestRequest(ResourceNames.Print, Method.POST);                
            _request.AddJsonBody(printFileInfo);

            var response = _client.Execute(_request);

            var responseData = new ResponseData()
            {
                ResponseCode = response.StatusCode.ToString(),
                ResponseBody = response.Content
            };

            return responseData;
        }
        catch (Exception ex)
        {
            _logger.Error(ex, string.Format(ErrorMessages.ResourceException, ResourceNames.Print));

            throw;
        }
    }

While sending through this C# client then I can see the IFormFile is filled in with data but when it goes to the API then it becomes null. Any suggestion would be appreciative.

user7336033
  • 271
  • 3
  • 16
  • can you share how you are calling the API ? – zetawars Jan 09 '20 at 12:06
  • `IFormFile`, as the name implies, works with form data requests, not JSON. – Nkosi Jan 09 '20 at 12:08
  • You are sending a PrintFileInfo on the request, but you are receiving a PrintFile on the API. They should either be the same class, or a compatible class (same properties) – Daniel Santos Jan 09 '20 at 12:09
  • you need to submit multipart data using HttpClient. have a look into this https://stackoverflow.com/questions/16416601/c-sharp-httpclient-4-5-multipart-form-data-upload – zetawars Jan 09 '20 at 12:40

2 Answers2

0

I don't think you can post an IFormFile like that; I believe it has to be a parameter to the action method, not a property to a parameter. If you really need to use complex parameters, it's better to load the whole file into that property, as either an array of bytes or a string.

Ricardo Peres
  • 13,724
  • 5
  • 57
  • 74
0

You use FromBody attribute but you need to use FromForm

[Route("print")]
        [HttpPost]
        public IHttpActionResult Print([FromForm] PrintFile printDocument)
        {
    }
Ivan Martinyuk
  • 1,230
  • 1
  • 9
  • 13