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

  • Question

  • User756776635 posted

    I 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.

    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
    }

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

    PS: This post had been previously moved to a forum that tells you which forum to move the post to without actually giving you a link. If this is not the right forum, could the moderator please provide a link? Thanks you.

    Monday, November 23, 2020 8:51 PM

All replies

  • User475983607 posted

    However, I am trying to implement an alternative solution where the files are added to a multipart content then to the response.

    It's not possible for a browser to download more than one file.   You can download a zip file.

    Monday, November 23, 2020 9:50 PM
  • User-474980206 posted

    there is no multi-part response support in asp.net core.  the json formatter is being used in your case. there has been a request, made, but its not likely as it is not well supported. you can make your own formatter. see the issue thread:

      https://github.com/aspnet/Mvc/issues/4933

    not sure your use case. as the multi-part response can only be handled by a custom client, its seems the zip format (which compresses the files) would be a better approach, as zip libraries are pretty common. 

                                         

    Monday, November 23, 2020 10:00 PM
  • User756776635 posted

    Thank you Bruce. My case is that there is an existing client whose code I don't have access to. I am replacing the existing MEAN API and I guess I have to spend more time understanding the Node code to see whether it returns a file at a time or a list of files. I also am writing my own client to test my code, obviously. 

    Tuesday, November 24, 2020 6:34 PM
  • User-474980206 posted

    how does the angular code do with the files? it can only use them in memory, as it can not write them to disk. if the browser is saving the files, it does not support a multi-part response.

    the express code should be pretty easy to read. obviously it can handle with the database data much easier than c#. 

    Tuesday, November 24, 2020 7:38 PM