有没有办法向 Javascript 添加回调并在 Blazor 中获取结果?除了 JS 承诺。
例如,假设我想加载一个文件
Javascript代码
window.readFile = function(filePath, callBack) {
var reader = new FileReader();
reader.onload = function (evt) {
callBack(evt.target.result);
};
reader.readAsText(filePath);
}
Run Code Online (Sandbox Code Playgroud)
我可以在 Blazor C# 中有这样的东西吗
window.readFile = function(filePath, callBack) {
var reader = new FileReader();
reader.onload = function (evt) {
callBack(evt.target.result);
};
reader.readAsText(filePath);
}
Run Code Online (Sandbox Code Playgroud)
或者也许是这样的
// read file content and output result to console
void GetFileContent() {
JsRuntime.InvokeAsync<object>("readFile", "file.txt", (string text) => {
Console.Write(text);
});
}
Run Code Online (Sandbox Code Playgroud)
谢谢