0

I'm sure somebody had the same problem, but I didn't find anything. I send post request to get the file, and I get this model as a response:

    public class ResponseWithFile
{
    public bool IsSuccessful { get; set; }
    public List<int> Errors { get; set; }
    public IFormFile File { get; set; }
}

I get this response from a controller:

    [Route("get")]
    [HttpPost]
    public async Task<IActionResult> GetFile([FromBody]GetFileDto request)
    {
        var result = _fileService.GetFile(request.Id, request.ContentType);
        if (result.IsSuccessful)
            return Ok(result);
        return BadRequest(result);
    }

The response is correct, I can read it into a string, but when I try to deserialize it into response object I get error:

            HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
        string respString = string.Empty;
        using (var sr = new StreamReader(resp.GetResponseStream()))
        {
            respString = sr.ReadToEnd();
        }

        var serResp = (ResponseWithFile)JsonConvert.DeserializeObject(respString);//error here

InvalidCastException: Unable to cast object of type 'Newtonsoft.Json.Linq.JObject' to type 'ServiceModels.ResponseWithFile

I'm sure it's because of IFormFile object. What am I doing wrong?

Jamil
  • 830
  • 2
  • 15
  • 34
  • nobody ever reads exception messages :( – vasily.sib Jul 22 '19 at 09:31
  • 1
    Isn't `IFormFile` used to represent a file that *uploaded* to a web service, i.e. when using a `
    `? `ResponseWithFile` suggests you try to use it as the response when *downloading* a file.
    – Dirk Jul 22 '19 at 09:31
  • Show us your `respString` JSON string so that we can see what data you are receiving. – Rahul Sharma Jul 22 '19 at 11:34

1 Answers1

1

Try this:

var serResp = JsonConvert.DeserializeObject<ResponseWithFile>(respString);

or

  var serResp = (ResponseWithFile)JsonConvert.DeserializeObject(respString, typeof(ResponseWithFile));
Dilshod K
  • 2,924
  • 1
  • 13
  • 46
  • 1
    now I get JsonSerializationException: Could not create an instance of type Microsoft.AspNetCore.Http.IFormFile. Type is an interface or abstract class and cannot be instantiated. looks like I shouldn't use IFormFile to receive files – Jamil Jul 22 '19 at 09:45
  • @Jamil, Look at https://stackoverflow.com/questions/2254872/using-json-net-converters-to-deserialize-properties. I believe, you will get answer – Dilshod K Jul 22 '19 at 09:49
  • 3
    @Jamil looks like you should start reading your exceptions messages. _"Could not create an instance of type Microsoft.AspNetCore.Http.IFormFile. Type is an interface or abstract class and cannot be instantiated"_ means that `IFormFile` is an **interface** and cannot be instantiated. No one can instantiate an instance of interface. – vasily.sib Jul 22 '19 at 10:12
  • The problem was that I was not reading from stream correctly, and I probably shouldn't use IFormFile there. I will accept this answer, because technically it is a solution to my problem(though problem was in a different place). – Jamil Jul 23 '19 at 07:50