Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk.Razor">

<PropertyGroup>
<Version>9.0.5</Version>
<Version>9.0.6</Version>
</PropertyGroup>

<PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public partial class Editor
/// 获得/设置 Editor 组件内上传文件时回调此方法
/// </summary>
[Parameter]
public Func<EditorUploadFile, Task>? OnFileUpload { get; set; }
public Func<EditorUploadFile, Task<string>>? OnFileUpload { get; set; }

private bool _lastShowSubmit = true;

Expand Down Expand Up @@ -270,14 +270,16 @@ public async Task<string> ClickPluginItem(string pluginItemName)
/// <param name="size"></param>
/// <param name="stream"></param>
[JSInvokable]
public async Task ImageUpload(string name, string contentType, long size, IJSStreamReference stream)
public async Task<string> ImageUpload(string name, string contentType, long size, IJSStreamReference stream)
{
var data = await stream.OpenReadStreamAsync(size);
string? ret = null;
await using var data = await stream.OpenReadStreamAsync(size);
var file = new EditorUploadFile(name, contentType, size, data);
if (OnFileUpload != null)
{
await OnFileUpload(file);
ret = await OnFileUpload(file);
}
return ret ?? "";
}
Comment on lines +273 to 283
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion (bug_risk): Returning an empty string may mask upload failures.

Consider returning null or a distinct error value instead, so client-side code can reliably detect upload failures and handle them appropriately.

Suggested change
public async Task<string> ImageUpload(string name, string contentType, long size, IJSStreamReference stream)
{
var data = await stream.OpenReadStreamAsync(size);
string? ret = null;
await using var data = await stream.OpenReadStreamAsync(size);
var file = new EditorUploadFile(name, contentType, size, data);
if (OnFileUpload != null)
{
await OnFileUpload(file);
ret = await OnFileUpload(file);
}
return ret ?? "";
}
public async Task<string?> ImageUpload(string name, string contentType, long size, IJSStreamReference stream)
{
string? ret = null;
await using var data = await stream.OpenReadStreamAsync(size);
var file = new EditorUploadFile(name, contentType, size, data);
if (OnFileUpload != null)
{
ret = await OnFileUpload(file);
}
return ret;
}


/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,17 +51,16 @@ export async function init(id, invoker, methodGetPluginAttrs, methodClickPluginI
editor.files = files
for (let i = 0; i < files.length; i++) {
const file = files[i];
const image = createImage(file);
editor.$editor.summernote('insertNode', image);

const buffer = await file.arrayBuffer();
const stream = DotNet.createJSStreamReference(buffer);
await editor.invoker.invokeMethodAsync('ImageUpload',
const url = await editor.invoker.invokeMethodAsync('ImageUpload',
file.name,
file.type || 'application/octet-stream',
file.size,
stream
)
const image = createImage(file, url);
editor.$editor.summernote('insertNode', image);
}
}
}
Expand Down Expand Up @@ -220,9 +219,9 @@ export function dispose(id) {
}
}

const createImage = file => {
const createImage = (file, url) => {
const element = document.createElement('img');
element.src = URL.createObjectURL(file);
element.src = url || URL.createObjectURL(file);
Comment on lines +222 to +224
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion (bug_risk): The fallback to URL.createObjectURL may leak object URLs if not revoked.

Add logic to revoke the object URL after the image loads to prevent memory leaks when using the fallback.

return element;
}

Expand Down