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

  • Question

  • 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 7:01 PM

All replies

  • Hi TheBugSlayer,
    This Visual C# forum mainly discusses and asks questions about the C# programming language, IDE, libraries, samples, and tools.
    Based on your description, it is recommended to ask the question on the ASP.NET Core forum and you can get more professional answer.
    Thank you for your understanding.
    Best Regards,
    Daniel Zhang


    "Visual c#" forum will be migrating to a new home on Microsoft Q&A ! We invite you to post new questions in the "Developing Universal Windows apps" forum’s new home on Microsoft Q&A ! For more information, please refer to the sticky post.

    Monday, November 23, 2020 6:19 AM