-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Christoph Decker
authored and
Christoph Decker
committed
Oct 28, 2024
1 parent
e3a2351
commit eb03a38
Showing
6 changed files
with
117 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
using Microsoft.JSInterop; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace chdTour.App.Implementations | ||
{ | ||
public class DownloadService : IDownloadService | ||
{ | ||
private readonly IJSRuntime _jSRuntime; | ||
|
||
public DownloadService(IJSRuntime jSRuntime) | ||
{ | ||
this._jSRuntime = jSRuntime; | ||
} | ||
|
||
public async Task DownloadFileFromStream(string name, byte[] data) | ||
{ | ||
var fileStream = GetFileStream(data); | ||
var fileName = name; | ||
|
||
using var streamRef = new DotNetStreamReference(stream: fileStream); | ||
await this._jSRuntime.InvokeVoidAsync("downloadFileFromStream", fileName, streamRef); | ||
} | ||
|
||
private Stream GetFileStream(byte[] data) | ||
{ | ||
var fileStream = new MemoryStream(data); | ||
return fileStream; | ||
} | ||
} | ||
public interface IDownloadService | ||
{ | ||
Task DownloadFileFromStream(string name, byte[] data); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
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); | ||
} |