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);
}