【发布时间】:2020-07-18 05:25:31
【问题描述】:
这几天我一直在思考这个问题,但无法弄清楚。如何在 Blazor Razor 组件中使用 pdf 的本地文件 url 创建下载链接。
@code {
string noteContent;
string fileName = "note.txt";
string path = @"E:\Attachments\test.txt";
Uri myUri = new Uri(@"E:\Attachments\test.txt");
public async void DownloadFile()
{
//await JSRuntime.InvokeAsync<object>(
// "FileSaveAs",
// fileName,
// noteContent
//);
await JSRuntime.InvokeAsync<object>(
"downloadURI",
myUri,
"test.txt"
);
}
}
和js文件
function FileSaveAs(filename, fileContent) {
var link = document.createElement('a');
link.download = filename;
link.href = "data:text/plain;charset=utf-8," +
encodeURIComponent(fileContent)
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
}
function downloadURI(uri, name) {
var link = document.createElement("a");
link.download = name;
link.href = uri;enter code here
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
delete link;
}
【问题讨论】:
-
你能分享一个minimal reproducible example你到目前为止的进展吗?显示您尝试过的内容?
-
Blazor 组件如何知道本地文件的路径?
-
我尝试过使用 JSRuntime.InvokeAsync