I am using Blazors InputFile component and I wish to upload and parse Json from a web view. Upon uploading a very large json file (11mb and ~300.000 lines) I got an exception that the json content is invalid. I tried putting a breakpoint on the line above, and it seems to be sliced somehow, so the end of the file is in the middle of an element. I checked the stream (see the code below) and after some index (around 100.000), every element has value of 0.
razor
<InputFile @ref="InputFile" OnChange="OnUploadHandler" />
razor.cs
async Task OnUploadHandler(InputFileChangeEventArgs e)
{
var stream = e.File.OpenReadStream(Int32.MaxValue);
Int32 length = stream.Length > Int32.MaxValue ? Int32.MaxValue : Convert.ToInt32(stream.Length);
Byte[] buffer = new Byte[length];
await stream.ReadAsync(buffer.AsMemory(0, length));
var content = Encoding.Default.GetString(buffer);
var parsed = JObject.Parse(content);
}
Consider a toy example. Suppose I upload a file lorem.json that has the following content:
[
{
"_id": "638dfa2f5e1dd23eb7b407e9",
"index": 0,
"guid": "89e63cb9-0498-4f5f-829e-9aaec0cf6855",
"address": "706 Irving Avenue, Leroy, Iowa, 6592",
"about": "Cupidatat adipisicing cupidatat culpa et labore id proident incididunt occaecat. Laboris velit fugiat consequat est sunt veniam ex. Ullamco irure ex laborum dolor esse anim et sunt in nulla ea aliquip. Ea proident irure cillum quis qui magna id ad ex qui. Veniam anim Lorem aliqua officia. Et cillum dolore ad aliqua ex voluptate culpa dolor occaecat cupidatat ex qui et enim. Elit irure voluptate non cupidatat aliqua duis in duis ut qui.\r\n",
"tags": [
"irure",
"tempor",
"irure",
"consectetur",
"duis",
"proident",
"ex"
],
"friends": [
{
"id": 0,
"name": "Zimmerman Burch"
},
{
"id": 1,
"name": "Snow Harrington"
}
],
"greeting": "Hello, undefined! You have 3 unread messages.",
"favoriteFruit": "strawberry"
},
{
"_id": "638dfa2f226b8c0be4f73fa9",
"index": 1,
"guid": "0a7b9bb4-580f-460e-b876-f0cf62260983",
"address": "241 Jamaica Avenue, Chicopee, Palau, 891",
"about": "Excepteur laboris reprehenderit consequat labore exercitation enim consequat. Ea cillum in nulla deserunt amet ipsum ad magna Lorem minim sunt veniam magna. Laboris voluptate aute ullamco labore dolor consectetur irure aute ea cillum aliquip esse dolor ex. Labore proident officia nostrud excepteur eu ad irure ipsum sint cupidatat eiusmod esse. Exercitation ea incididunt consequat laborum cillum incididunt reprehenderit exercitation enim nostrud elit dolor pariatur ex. Magna cillum Lorem deserunt minim ad irure do sit non velit qui culpa veniam occaecat. Sunt culpa non laborum Lorem nisi ea laborum proident occaecat pariatur aliquip irure eu.\r\n",
"tags": [
"aute",
"labore",
"consequat",
"do",
"est",
"labore",
"ex"
],
"friends": [
{
"id": 0,
"name": "Johanna Dunn"
},
{
"id": 1,
"name": "Prince Estes"
}
],
"greeting": "Hello, undefined! You have 7 unread messages.",
"favoriteFruit": "strawberry"
}
]
in the code above, if I were to put a breakpoint and look at the content variable, I would find it to be something like:
[
{
"_id": "638dfa2f5e1dd23eb7b407e9",
"index": 0,
"guid": "89e63cb9-0498-4f5f-829e-9aaec0cf6855",
"address": "706 Irving Avenue, Leroy, Iowa, 6592",
"about": "Cupidatat adipisicing cupidatat culpa et labore id proident incididunt occaecat. Laboris velit fugiat consequat est sunt veniam ex. Ullamco irure ex laborum dolor esse anim et sunt in nulla ea aliquip. Ea proident irure cillum quis qui magna id ad ex qui. Veniam anim Lorem aliqua officia. Et cillum dolore ad aliqua ex voluptate culpa dolor occaecat cupidatat ex qui et enim. Elit irure voluptate non cupidatat aliqua duis in duis ut qui.\r\n",
"tags": [
"irure",
"tempor",
"irure",
"consectetur",
"duis",
"proident",
"ex"
],
"friends": [
{
"id": 0,
"name": "Zimmerman Burch"
},
{
"id": 1,
"name": "Snow Harrington"
}
],
"greeting": "Hello, undefined! You have 3 unread messages.",
"favoriteFruit": "strawberry"
},
{
"_id": "638dfa2f226b8c0be4f73fa9",
"index": 1,
"guid": "0a7b9bb4-580f-460e-b876-f0cf62260983",