locked
.Net Core Web API - uploading multiple files. Postman returns empty Json. RRS feed

  • Question

  • Hello,

    Have written code to upload multiple files through one zip file and it works. In Postman I am able to download the zip file. However, I am trying to implement an alternative solution where the files are added to a multipart content then to the response. The code works...but in Postman I receive an OK status but the body of the response is empty.

    Please help me figure out what I am missing or doing wrong. Thank you.

    C# CODE

            [HttpGet("/results2/{resultcount}")]
            public ActionResult GetFiles(int results)
            {
                var fi = ReadFiles(results);
    
                if (fi.Count() > 0)
                {
                    var content = new MultipartContent();
    
    
                    var objectContent = new StringContent(Newtonsoft.Json.JsonConvert.SerializeObject(fi.Select(f => f.FullName)));
                    content.Add(objectContent);
    
                    foreach (FileInfo f in fi)
                    {
                        var fileContent = new StreamContent(new FileStream(f.FullName, FileMode.Open));
                        fileContent.Headers.ContentType = System.Net.Http.Headers.MediaTypeHeaderValue.Parse("text/plain");
                        fileContent.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment");
                        content.Add(fileContent);
                     }
    
                    var response = new HttpResponseMessage();
                    response.Content = content;
                    return Ok(response);
                }
                else 
                    return BadRequest("Number of files must be greater than zero (0)!");
            }

    RESPONSE

    {
        "version": {
            "major": 1,
            "minor": 1,
            "build": -1,
            "revision": -1,
            "majorRevision": -1,
            "minorRevision": -1
        },
        "content": {
            "headers": [
                {
                    "Key": "Content-Type",
                    "Value": [
                        "multipart/mixed; boundary=\"686d1a49-9fd9-4342-90e6-c35f5b9274b6\""
                    ]
                }
            ]
        },
        "statusCode": 200,
        "reasonPhrase": "OK",
        "headers": [],
        "trailingHeaders": [],
        "requestMessage": null,
        "isSuccessStatusCode": true
    }


    TheBugSlayer

    Friday, November 20, 2020 4:39 PM

All replies

  • ASP.NET WebAPI has forum in ASP.NET forums.

    https://forums.asp.net/

    Friday, November 20, 2020 5:40 PM
  • I will move the post to that forum. Thank you!

    TheBugSlayer

    Friday, November 20, 2020 6:43 PM