-
-
Notifications
You must be signed in to change notification settings - Fork 7
feat(Editor): add OnFileUpload parameter #532
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
1bf508a
ac0cea0
f15eafd
98f34e9
1101bb2
c43af81
f810deb
8bc2ab4
7222d12
231e9e2
d9189d9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,115 @@ | ||||||||
| // Copyright (c) Argo Zhang (argo@163.com). All rights reserved. | ||||||||
| // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||||||||
| // Website: https://www.blazor.zone or https://argozhang.github.io/ | ||||||||
|
|
||||||||
| namespace BootstrapBlazor.Components; | ||||||||
|
|
||||||||
| /// <summary> | ||||||||
| /// 文件信息 | ||||||||
| /// </summary> | ||||||||
| public class EditorUploadFile | ||||||||
| { | ||||||||
| /// <summary> | ||||||||
| /// 文件名 | ||||||||
| /// </summary> | ||||||||
| public string? FileName { get; set; } | ||||||||
|
|
||||||||
| /// <summary> | ||||||||
| /// 文件大小 | ||||||||
| /// </summary> | ||||||||
| public long FileSize { get; set; } | ||||||||
|
|
||||||||
| /// <summary> | ||||||||
| /// 最后修改日期 | ||||||||
| /// </summary> | ||||||||
| public string? LastModified { get; set; } | ||||||||
|
|
||||||||
| /// <summary> | ||||||||
| /// 文件类型 | ||||||||
| /// </summary> | ||||||||
| public string? ContentType { get; set; } | ||||||||
|
|
||||||||
| /// <summary> | ||||||||
| /// 上传的文件流 | ||||||||
| /// </summary> | ||||||||
| public Stream? UploadStream { get; set; } | ||||||||
|
|
||||||||
| /// <summary> | ||||||||
| /// 文件索引 | ||||||||
| /// <para>用于标识文件在上传列表中的位置</para> | ||||||||
| /// <para>例如在上传多个文件时可以通过此属性区分每个文件的索引位置</para> | ||||||||
| /// </summary> | ||||||||
| public int Index { get; set; } | ||||||||
|
|
||||||||
| /// <summary> | ||||||||
| /// 返回码,0为成功,非0失败 | ||||||||
| /// </summary> | ||||||||
| public int Code { get; set; } | ||||||||
|
|
||||||||
| /// <summary> | ||||||||
| /// 错误信息 | ||||||||
| /// </summary> | ||||||||
| public string? Error { get; set; } | ||||||||
|
|
||||||||
| /// <summary> | ||||||||
| /// 保存到文件 | ||||||||
| /// </summary> | ||||||||
| /// <param name="fileName"></param> | ||||||||
| /// <param name="token"></param> | ||||||||
| /// <returns></returns> | ||||||||
| public async Task<bool> SaveToFile(string fileName, CancellationToken token = default) | ||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. issue (complexity): Consider simplifying the SaveToFile method by using built-in file and stream operations to handle folder creation, file overwriting, and stream copying. Here’s a drastically simplified version of public async Task<bool> SaveToFile(string filePath, CancellationToken token = default)
{
try
{
// ensure folder exists (no need to check Exists first)
var folder = Path.GetDirectoryName(filePath);
if (!string.IsNullOrEmpty(folder))
Directory.CreateDirectory(folder);
// File.Create will truncate/overwrite existing file
using var output = File.Create(filePath);
if (UploadStream != null)
{
// CopyToAsync handles the buffer loops for you
await UploadStream.CopyToAsync(output, 81920, token);
}
return true;
}
catch (Exception ex)
{
// you can distinguish codes if you really need to
Code = 1003;
Error = ex.Message;
return false;
}
}Steps to apply:
|
||||||||
| { | ||||||||
| var ret = false; | ||||||||
| if (UploadStream != null) | ||||||||
| { | ||||||||
| // 文件保护,如果文件存在则先删除 | ||||||||
| if (System.IO.File.Exists(fileName)) | ||||||||
| { | ||||||||
| try | ||||||||
| { | ||||||||
| System.IO.File.Delete(fileName); | ||||||||
| } | ||||||||
| catch (Exception ex) | ||||||||
| { | ||||||||
| Code = 1002; | ||||||||
| Error = ex.Message; | ||||||||
| } | ||||||||
| } | ||||||||
|
|
||||||||
| var folder = Path.GetDirectoryName(fileName); | ||||||||
| if (!string.IsNullOrEmpty(folder) && !Directory.Exists(folder)) | ||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. issue: Directory creation does not handle exceptions. Wrap Directory.CreateDirectory(folder) in a try-catch block to handle potential exceptions and update Code and Error as needed. |
||||||||
| { | ||||||||
| Directory.CreateDirectory(folder); | ||||||||
| } | ||||||||
|
|
||||||||
| if (Code == 0) | ||||||||
| { | ||||||||
| using var uploadFile = File.OpenWrite(fileName); | ||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. issue (bug_risk): File.OpenWrite is not wrapped in a try-catch block. Move File.OpenWrite inside the try-catch block to handle exceptions and ensure Code/Error are set appropriately. |
||||||||
|
|
||||||||
| try | ||||||||
| { | ||||||||
| // 打开文件流 | ||||||||
| var stream = UploadStream; | ||||||||
|
|
||||||||
| var buffer = new byte[4 * 1096]; | ||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion: Buffer size calculation uses 1096 instead of 1024. Please confirm whether 1096 is intentional or if 1024 was intended, as powers of two are standard for buffer sizes.
Suggested change
|
||||||||
| int bytesRead = 0; | ||||||||
|
|
||||||||
| // 开始读取文件 | ||||||||
| while ((bytesRead = await stream.ReadAsync(buffer, token)) > 0) | ||||||||
| { | ||||||||
| await uploadFile.WriteAsync(buffer.AsMemory(0, bytesRead), token); | ||||||||
|
|
||||||||
| } | ||||||||
| ret = true; | ||||||||
| } | ||||||||
| catch (Exception ex) | ||||||||
| { | ||||||||
| Code = 1003; | ||||||||
| Error = ex.Message; | ||||||||
| } | ||||||||
| } | ||||||||
| } | ||||||||
| return ret; | ||||||||
| } | ||||||||
|
|
||||||||
| } | ||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion: lastModified conversion may not be accurate for all browsers.
Add a check to verify that file.lastModified is a valid number before converting it to an ISO string, as its type or presence may vary across browsers.