0

I have below json string that I'm passing onto my server-side code:

[{"PaymentMethod":"Cash","PaymentOrigin":"","PaymentReferenceNo":"123","ProductAmount":"123","PaymentNotes":"","PaymentAttachments":{"0":{},"1":{}}}]

PaymentAttachments are .txt files I'm trying to save in the server's file system. However, I'm unable to access it.

I tried to convert it to HttpFileCollection and HttpPostedFile however, I keep getting below error:

Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Web.HttpFileCollection' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.
To fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List<T>) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object.
Path '[0].PaymentAttachments.0', line 1, position 149.

Would like to know some good way to access these files in this json string format.

This is how I'm deserializing above json string:

var paymentCollectionObj = (Newtonsoft.Json.JsonConvert.DeserializeObject<List<PaymentDetails>>(paymentCollection));

Payment Details: (EDIT)

public string DatePaid { get; set; }
    public string PaymentOrigin { get; set; }
    public string PaymentReferenceNo { get; set; }
    public string ProductAmount { get; set; }
    public string PaymentNotes { get; set; }
    public HttpCollection PaymentAttachments { get; set; }

I wanted to convert it to HttpCollection so that I can do something like this:

foreach(string file in context.Request.Files)
            {
                var fileContent = context.Request.Files[file];
                
                var path = HttpContext.Current.Server.MapPath("../d");
                fileContent.SaveAs(path + fileContent.FileName);
            }
maj
  • 3
  • 4
  • Does this answer your question? [upload file on server webfolder and post json data at the same using asp.net web api](https://stackoverflow.com/questions/64479129/upload-file-on-server-webfolder-and-post-json-data-at-the-same-using-asp-net-web) – Chetan Mar 03 '21 at 14:37
  • Are you trying to post the actual file or just details of the files here? – DavidG Mar 03 '21 at 14:41
  • hi, thanks for this! unfortunately, I'm unable to use this because I'm currently working on asp.net 4 – maj Mar 03 '21 at 14:41
  • @DavidG, hi, I'm trying to save the actual file in the server's file system – maj Mar 03 '21 at 14:43
  • Please take some time to explain EXACTLY what you are doing. Just repeating the words in your question don't help. – DavidG Mar 03 '21 at 14:44
  • hi @DavidG, I edited my question and added what I'm trying to do. thank you! – maj Mar 03 '21 at 14:47

0 Answers0