2020-07-01 06:49:51 +08:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using Microsoft.AspNetCore.Components;
|
|
|
|
|
using Microsoft.JSInterop;
|
|
|
|
|
|
|
|
|
|
namespace AntDesign
|
|
|
|
|
{
|
|
|
|
|
public partial class Upload : AntDomComponentBase
|
|
|
|
|
{
|
|
|
|
|
[Parameter]
|
|
|
|
|
public Func<UploadFileItem, bool> BeforeUpload { get; set; }
|
|
|
|
|
|
|
|
|
|
[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; }
|
|
|
|
|
|
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-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
|
|
|
|
|
2020-07-01 06:49:51 +08:00
|
|
|
|
protected override Task OnInitializedAsync()
|
|
|
|
|
{
|
|
|
|
|
FileList.InsertRange(0, DefaultFileList);
|
|
|
|
|
return base.OnInitializedAsync();
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
}
|
2020-07-01 06:49:51 +08:00
|
|
|
|
}
|
|
|
|
|
}
|