[英]Blazor Download XML File with Javascript Error
我用我的應用程序生成了一個 XML 文件,我嘗試下載它,感謝 blazor WebAssembly 客戶端的 javascript。
我用的是微軟給的方法,XDocument保存在一個Memory Stream中:
async Task DownloadFileFromStream(MemoryStream Stream,string Name)
{
var fileStream = Stream;
var fileName = Name + ".xml";
using var streamRef = new DotNetStreamReference(stream : fileStream);
await JS.InvokeVoidAsync("downloadFileFromStream",fileName, streamRef);
}
以及我的 index.html 文件中與之關聯的 javascript function:
<script type="text/javascript">
window.downloadFileFromStream = async (fileName, contentStreamReference) => {
const arrayBuffer = await contentStreamReference.arrayBuffer();
const blob = new Blob([arrayBuffer]);
const url = URL.createObjectURL(blob);
const anchorElement = document.createElement('a');
anchorElement.href = url;
anchorElement.download = fileName ?? '';
anchorElement.click();
anchorElement.remove();
URL.revokeObjectURL(url);
}
</script>
但是每次調用 InvokeVoidAsync 時都會顯示此錯誤:
Microsoft.JSInterop.JSException: Failed to fetch
TypeError: Failed to fetch
at Microsoft.JSInterop.JSRuntime.
<InvokeAsync>d__16`1[[Microsoft.JSInterop.Infrastructure.IJSVoidResult,
Microsoft.JSInterop, Version=6.0.0.0, Culture=neutral,
PublicKeyToken=adb9793829ddae60]].MoveNext()
at Microsoft.JSInterop.JSRuntimeExtensions.InvokeVoidAsync(IJSRuntime jsRuntime, String
identifier, Object[] args)
我不明白為什么因為我使用了微軟提供的相同的 function。
感謝您的答復
我發現了問題所在。
當您使用 javascript function 時,您在 Do.netStreamReference 中轉換的 stream 需要打開。 我的被 class 中的返回關閉,其中生成了 xml。
我只是將它從 883812976388 代 class 中移出以刪除返回並將其放入與 javascript function 相同的 class 中並且它起作用了。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.