Skip to content

Commit 80a50e2

Browse files
committed
feat: 增加 DownloadAsync 方法
1 parent d8a7a75 commit 80a50e2

3 files changed

Lines changed: 48 additions & 4 deletions

File tree

src/components/BootstrapBlazor.Dom2Image/Services/DefaultDom2ImageService.cs

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class DefaultDom2ImageService(IJSRuntime runtime, ILogger<DefaultDom2ImageServic
3030
catch (OperationCanceledException) { }
3131
catch (Exception ex)
3232
{
33-
logger.LogError(ex, "{GetUrlAsync} throw exception", nameof(GetUrlAsync));
33+
logger.LogError(ex, "{GetUrlAsync} throw exception: {ex}", nameof(GetUrlAsync), ex.Format());
3434
}
3535
return data;
3636
}
@@ -53,10 +53,33 @@ class DefaultDom2ImageService(IJSRuntime runtime, ILogger<DefaultDom2ImageServic
5353
catch (OperationCanceledException) { }
5454
catch (Exception ex)
5555
{
56-
logger.LogError(ex, "{GetUrlAsync} throw exception", nameof(GetUrlAsync));
56+
logger.LogError(ex, "{GetStreamAsync} throw exception: {ex}", nameof(GetStreamAsync), ex.Format());
5757
}
5858
return data;
5959
}
6060

61+
/// <summary>
62+
/// <inheritdoc/>
63+
/// </summary>
64+
/// <param name="selector"></param>
65+
/// <param name="fileName"></param>
66+
/// <param name="format"></param>
67+
/// <param name="backgroundColor"></param>
68+
/// <param name="options"></param>
69+
/// <returns></returns>
70+
public async Task DownloadAsync(string selector, string fileName = "capture", string? format = "png", string? backgroundColor = null, Dom2ImageOptions? options = null)
71+
{
72+
try
73+
{
74+
_jsModule ??= await LoadModule();
75+
await _jsModule.InvokeAsync<IJSStreamReference?>("downloadAsync", selector, fileName, format, backgroundColor, options);
76+
}
77+
catch (OperationCanceledException) { }
78+
catch (Exception ex)
79+
{
80+
logger.LogError(ex, "{DownloadAsync} throw exception: {ex}", nameof(DownloadAsync), ex.Format());
81+
}
82+
}
83+
6184
private Task<JSModule> LoadModule() => runtime.LoadModule("./_content/BootstrapBlazor.Dom2Image/dom2image.js");
6285
}

src/components/BootstrapBlazor.Dom2Image/Services/IDom2ImageService.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,15 @@ public interface IDom2ImageService
2626
/// <param name="token"></param>
2727
/// <returns></returns>
2828
Task<Stream?> GetStreamAsync(string selector, Dom2ImageOptions? options = null, CancellationToken token = default);
29+
30+
/// <summary>
31+
/// 通过指定选择器下载 Html 元素图片
32+
/// </summary>
33+
/// <param name="selector"></param>
34+
/// <param name="fileName"></param>
35+
/// <param name="format"></param>
36+
/// <param name="backgroundColor"></param>
37+
/// <param name="options"></param>
38+
/// <returns></returns>
39+
Task DownloadAsync(string selector, string fileName = "capture", string? format = "png", string? backgroundColor = null, Dom2ImageOptions? options = null);
2940
}

src/components/BootstrapBlazor.Dom2Image/wwwroot/dom2image.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ export async function getUrl(selector, options = {}) {
44
let data = null;
55
const el = document.querySelector(selector);
66
if (el) {
7-
options.embedFonts = true;
87
const result = await snapdom(el, options);
98
data = result.url;
109
}
@@ -15,9 +14,20 @@ export async function getStream(selector, options = {}) {
1514
let data = null;
1615
const el = document.querySelector(selector);
1716
if (el) {
18-
options.embedFonts = true;
1917
const result = await snapdom(el, options);
2018
data = result.toBlob();
2119
}
2220
return data;
2321
}
22+
23+
export async function downloadAsync(selector, filename, format, backgroundColor, options) {
24+
const el = document.querySelector(selector);
25+
if (el) {
26+
const result = await snapdom(el, options);
27+
data = result.download({
28+
format,
29+
filename,
30+
backgroundColor
31+
});
32+
}
33+
}

0 commit comments

Comments
 (0)