Please check the code below. I want fullpath in place of filename that is passed, to parse json file.
I understand that InputFile does not provide full path info about the user file system that could potentially be exploited in some ways. But I have to pass this chosen file's full path from razor to index.js.
Is it possible that InputFile has this fullpath in memory and this full path memory variable can be passed and accessed in index.js without revealing the actual file path to the user? Thank you.
Code below:
razor page:
<button onclick="document.getElementById('flpicker').click()">choose file</button>
<InputFile id="flpicker" OnChange="@OnInputFileChange" hidden multiple />
private async Task OnInputFileChange(InputFileChangeEventArgs e)
{
foreach (var File in e.GetMultipleFiles(e.FileCount))
{
var fileName = File.Name;
}
await JSRuntime.InvokeVoidAsync("Create", fileName);
}
index.js:
function create(fileName){
$.getJSON(fileName, function (data) {
....
});
}
Edited:
AppState.cs:
public string? json
{
get;
set;
}
page1.razor:
private IBrowserFile _loadedFile;
private async Task OnInputFileChange(InputFileChangeEventArgs e)
{
_loadedFile = e.File;
var utf8Json = _loadedFile.OpenReadStream(maxAllowedSize: 1024 * 1024 * 10);
try
{
var jsonDocument = await JsonDocument.ParseAsync(utf8Json);
AppState.json = jsonDocument @* I understand can't convert to string *@
}
catch (JsonException ex)
{
Console.WriteLine(ex.Message);
}
}
page2.razor:
protected override async Task OnAfterRenderAsync(bool firstRender)
{
await JSRuntime.InvokeVoidAsync("Create", AppState.json);
}