2020-07-01 06:49:51 +08:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using Microsoft.AspNetCore.Components;
|
2021-07-24 21:29:31 +08:00
|
|
|
|
using Microsoft.AspNetCore.Components.Web;
|
2020-07-01 06:49:51 +08:00
|
|
|
|
|
|
|
|
|
namespace AntDesign
|
|
|
|
|
{
|
|
|
|
|
public partial class Upload : AntDomComponentBase
|
|
|
|
|
{
|
|
|
|
|
[Parameter]
|
|
|
|
|
public Func<UploadFileItem, bool> BeforeUpload { get; set; }
|
|
|
|
|
|
2021-04-03 00:45:55 +08:00
|
|
|
|
[Parameter]
|
|
|
|
|
public Func<List<UploadFileItem>, Task<bool>> BeforeAllUploadAsync { get; set; }
|
|
|
|
|
|
|
|
|
|
[Parameter]
|
|
|
|
|
public Func<List<UploadFileItem>, bool> BeforeAllUpload { get; set; }
|
|
|
|
|
|
2020-07-01 06:49:51 +08:00
|
|
|
|
[Parameter]
|
|
|
|
|
public string Name { get; set; }
|
|
|
|
|
|
|
|
|
|
[Parameter]
|
|
|
|
|
public string Action { get; set; }
|
|
|
|
|
|
|
|
|
|
[Parameter]
|
2021-01-19 21:18:50 +08:00
|
|
|
|
public bool Disabled { get; set; }
|
2020-07-01 06:49:51 +08:00
|
|
|
|
|
2020-07-21 15:50:38 +08:00
|
|
|
|
[Parameter]
|
|
|
|
|
public Dictionary<string, object> Data { get; set; }
|
|
|
|
|
|
2020-07-21 00:16:02 +08:00
|
|
|
|
[Parameter]
|
|
|
|
|
public string ListType { get; set; } = "text";
|
|
|
|
|
|
|
|
|
|
[Parameter]
|
|
|
|
|
public bool Directory { get; set; }
|
|
|
|
|
|
|
|
|
|
[Parameter]
|
|
|
|
|
public bool Multiple { get; set; }
|
|
|
|
|
|
2020-07-01 06:49:51 +08:00
|
|
|
|
[Parameter]
|
|
|
|
|
public string Accept { get; set; }
|
|
|
|
|
|
2020-07-21 00:16:02 +08:00
|
|
|
|
[Parameter]
|
|
|
|
|
public bool ShowUploadList { get; set; } = true;
|
|
|
|
|
|
2020-07-01 06:49:51 +08:00
|
|
|
|
[Parameter]
|
|
|
|
|
public List<UploadFileItem> FileList { get; set; } = new List<UploadFileItem>();
|
|
|
|
|
|
2021-01-19 21:18:50 +08:00
|
|
|
|
[Parameter]
|
|
|
|
|
public UploadLocale Locale { get; set; } = LocaleProvider.CurrentLocale.Upload;
|
|
|
|
|
|
2020-09-02 15:15:59 +08:00
|
|
|
|
[Parameter]
|
|
|
|
|
public EventCallback<List<UploadFileItem>> FileListChanged { get; set; }
|
|
|
|
|
|
2020-07-01 06:49:51 +08:00
|
|
|
|
[Parameter]
|
|
|
|
|
public List<UploadFileItem> DefaultFileList { get; set; } = new List<UploadFileItem>();
|
|
|
|
|
|
|
|
|
|
[Parameter]
|
|
|
|
|
public Dictionary<string, string> Headers { get; set; }
|
|
|
|
|
|
|
|
|
|
[Parameter]
|
|
|
|
|
public EventCallback<UploadInfo> OnSingleCompleted { get; set; }
|
|
|
|
|
|
|
|
|
|
[Parameter]
|
|
|
|
|
public EventCallback<UploadInfo> OnCompleted { get; set; }
|
|
|
|
|
|
|
|
|
|
[Parameter]
|
|
|
|
|
public EventCallback<UploadInfo> OnChange { get; set; }
|
|
|
|
|
|
2020-07-29 11:26:20 +08:00
|
|
|
|
[Parameter]
|
|
|
|
|
public Func<UploadFileItem, Task<bool>> OnRemove { get; set; }
|
|
|
|
|
|
2020-07-31 00:46:46 +08:00
|
|
|
|
[Parameter]
|
|
|
|
|
public EventCallback<UploadFileItem> OnPreview { get; set; }
|
|
|
|
|
|
2021-04-03 00:45:55 +08:00
|
|
|
|
[Parameter]
|
|
|
|
|
public EventCallback<UploadFileItem> OnDownload { get; set; }
|
|
|
|
|
|
2020-07-01 06:49:51 +08:00
|
|
|
|
[Parameter]
|
|
|
|
|
public RenderFragment ChildContent { get; set; }
|
|
|
|
|
|
2020-07-31 00:46:46 +08:00
|
|
|
|
[Parameter]
|
|
|
|
|
public bool ShowButton { get; set; } = true;
|
|
|
|
|
|
2021-07-24 21:29:31 +08:00
|
|
|
|
[Parameter]
|
|
|
|
|
public bool Drag { get; set; }
|
|
|
|
|
|
2021-01-19 21:18:50 +08:00
|
|
|
|
private bool IsText => ListType == "text";
|
|
|
|
|
private bool IsPicture => ListType == "picture";
|
|
|
|
|
private bool IsPictureCard => ListType == "picture-card";
|
2020-09-02 15:15:59 +08:00
|
|
|
|
|
2021-03-12 17:02:11 +08:00
|
|
|
|
private ClassMapper _listClassMapper = new ClassMapper();
|
|
|
|
|
|
2021-07-24 21:29:31 +08:00
|
|
|
|
internal bool _dragHover;
|
|
|
|
|
|
2021-03-12 17:02:11 +08:00
|
|
|
|
protected override void OnInitialized()
|
|
|
|
|
{
|
|
|
|
|
base.OnInitialized();
|
|
|
|
|
|
|
|
|
|
var prefixCls = "ant-upload";
|
|
|
|
|
|
|
|
|
|
_listClassMapper
|
|
|
|
|
.Add($"{prefixCls}-list")
|
|
|
|
|
.Get(() => $"{prefixCls}-list-{ListType}")
|
|
|
|
|
.If($"{prefixCls}-list-rtl", () => RTL);
|
2021-07-24 21:29:31 +08:00
|
|
|
|
|
|
|
|
|
FileList.InsertRange(0, DefaultFileList);
|
2021-03-12 17:02:11 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-07-24 21:29:31 +08:00
|
|
|
|
private void HandleDrag(DragEventArgs args)
|
2020-07-01 06:49:51 +08:00
|
|
|
|
{
|
2021-07-24 21:29:31 +08:00
|
|
|
|
_dragHover = args.Type == "dragover";
|
2020-07-01 06:49:51 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-07-29 11:26:20 +08:00
|
|
|
|
private async Task RemoveFile(UploadFileItem item)
|
|
|
|
|
{
|
2020-10-09 11:32:32 +08:00
|
|
|
|
var canRemove = OnRemove == null || await OnRemove.Invoke(item);
|
2020-07-29 11:26:20 +08:00
|
|
|
|
if (canRemove)
|
|
|
|
|
{
|
|
|
|
|
this.FileList.Remove(item);
|
2020-09-02 15:15:59 +08:00
|
|
|
|
await this.FileListChanged.InvokeAsync(this.FileList);
|
2020-07-29 11:26:20 +08:00
|
|
|
|
StateHasChanged();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-19 21:18:50 +08:00
|
|
|
|
private async Task PreviewFile(UploadFileItem item)
|
2020-07-01 06:49:51 +08:00
|
|
|
|
{
|
2021-01-19 21:18:50 +08:00
|
|
|
|
if (item.State == UploadState.Success && OnPreview.HasDelegate)
|
2020-07-01 06:49:51 +08:00
|
|
|
|
{
|
2021-01-19 21:18:50 +08:00
|
|
|
|
await OnPreview.InvokeAsync(item);
|
2020-07-01 06:49:51 +08:00
|
|
|
|
}
|
2020-07-21 00:16:02 +08:00
|
|
|
|
}
|
2021-04-03 00:45:55 +08:00
|
|
|
|
|
|
|
|
|
private async Task DownloadFile(UploadFileItem item)
|
|
|
|
|
{
|
|
|
|
|
if (item.State == UploadState.Success && OnDownload.HasDelegate)
|
|
|
|
|
{
|
|
|
|
|
await OnDownload.InvokeAsync(item);
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-07-01 06:49:51 +08:00
|
|
|
|
}
|
|
|
|
|
}
|