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
|
|
|
|
|
{
|
2020-07-21 12:53:56 +08:00
|
|
|
|
private bool _disabled;
|
|
|
|
|
|
|
|
|
|
private bool _disabledChanged;
|
|
|
|
|
|
2020-07-01 06:49:51 +08:00
|
|
|
|
[Parameter]
|
|
|
|
|
public Func<UploadFileItem, bool> BeforeUpload { get; set; }
|
|
|
|
|
|
|
|
|
|
[Parameter]
|
|
|
|
|
public string Name { get; set; }
|
|
|
|
|
|
|
|
|
|
[Parameter]
|
|
|
|
|
public string Action { get; set; }
|
|
|
|
|
|
|
|
|
|
[Parameter]
|
2020-07-21 12:53:56 +08:00
|
|
|
|
public bool Disabled
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return _disabled;
|
|
|
|
|
}
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
_disabledChanged = value != _disabled;
|
|
|
|
|
_disabled = value;
|
|
|
|
|
}
|
|
|
|
|
}
|
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>();
|
|
|
|
|
|
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;
|
|
|
|
|
|
2020-07-01 06:49:51 +08:00
|
|
|
|
[Inject]
|
|
|
|
|
public IJSRuntime JSRuntime { get; set; }
|
|
|
|
|
|
|
|
|
|
private DotNetObjectReference<Upload> _currentInstance;
|
|
|
|
|
|
|
|
|
|
private UploadInfo _uploadInfo = new UploadInfo();
|
|
|
|
|
|
|
|
|
|
public int Progress { get; set; }
|
|
|
|
|
|
|
|
|
|
private ElementReference _file;
|
|
|
|
|
|
2020-07-02 14:08:51 +08:00
|
|
|
|
private ElementReference _btn;
|
|
|
|
|
|
|
|
|
|
private string _fileId = Guid.NewGuid().ToString();
|
|
|
|
|
|
2020-09-02 15:15:59 +08:00
|
|
|
|
private bool _beforeTheFirstRender = false;
|
|
|
|
|
|
2020-07-01 06:49:51 +08:00
|
|
|
|
protected override Task OnInitializedAsync()
|
|
|
|
|
{
|
|
|
|
|
_currentInstance = DotNetObjectReference.Create(this);
|
|
|
|
|
_uploadInfo.FileList = FileList;
|
|
|
|
|
FileList.InsertRange(0, DefaultFileList);
|
|
|
|
|
return base.OnInitializedAsync();
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-21 00:16:02 +08:00
|
|
|
|
protected override async Task OnAfterRenderAsync(bool firstRender)
|
2020-07-01 06:49:51 +08:00
|
|
|
|
{
|
2020-09-02 15:15:59 +08:00
|
|
|
|
if (firstRender)
|
|
|
|
|
{
|
|
|
|
|
_beforeTheFirstRender = true;
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-21 00:16:02 +08:00
|
|
|
|
if (firstRender && !Disabled)
|
2020-07-02 14:08:51 +08:00
|
|
|
|
{
|
2020-09-09 00:52:14 +08:00
|
|
|
|
await JSRuntime.InvokeVoidAsync(JSInteropConstants.AddFileClickEventListener, _btn);
|
2020-07-02 14:08:51 +08:00
|
|
|
|
}
|
2020-09-02 15:15:59 +08:00
|
|
|
|
|
|
|
|
|
if (_beforeTheFirstRender && _disabledChanged)
|
2020-07-21 12:53:56 +08:00
|
|
|
|
{
|
|
|
|
|
_disabledChanged = false;
|
|
|
|
|
if (Disabled)
|
|
|
|
|
{
|
2020-09-09 00:52:14 +08:00
|
|
|
|
await JSRuntime.InvokeVoidAsync(JSInteropConstants.RemoveFileClickEventListener, _btn);
|
2020-07-21 12:53:56 +08:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2020-09-09 00:52:14 +08:00
|
|
|
|
await JSRuntime.InvokeVoidAsync(JSInteropConstants.AddFileClickEventListener, _btn);
|
2020-07-21 12:53:56 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2020-07-21 00:16:02 +08:00
|
|
|
|
await base.OnAfterRenderAsync(firstRender);
|
2020-07-01 06:49:51 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async Task FileNameChanged(ChangeEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
var value = e.Value?.ToString();
|
|
|
|
|
if (string.IsNullOrWhiteSpace(value))
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
2020-09-09 00:52:14 +08:00
|
|
|
|
var flist = await JSRuntime.InvokeAsync<List<UploadFileItem>>(JSInteropConstants.GetFileInfo, _file);
|
2020-07-21 00:16:02 +08:00
|
|
|
|
var index = 0;
|
|
|
|
|
foreach (var fileItem in flist)
|
2020-07-01 06:49:51 +08:00
|
|
|
|
{
|
2020-07-21 00:16:02 +08:00
|
|
|
|
var fileName = fileItem.FileName;
|
|
|
|
|
fileItem.Ext = fileItem.FileName.Substring(fileName.LastIndexOf('.'));
|
|
|
|
|
var id = Guid.NewGuid().ToString();
|
|
|
|
|
if (BeforeUpload != null)
|
2020-07-01 06:49:51 +08:00
|
|
|
|
{
|
2020-07-21 00:16:02 +08:00
|
|
|
|
if (!BeforeUpload.Invoke(fileItem))
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
2020-07-01 06:49:51 +08:00
|
|
|
|
}
|
2020-07-21 00:16:02 +08:00
|
|
|
|
fileItem.Progress = 0;
|
|
|
|
|
fileItem.State = UploadState.Uploading;
|
|
|
|
|
fileItem.Id = id;
|
|
|
|
|
FileList.Add(fileItem);
|
2020-09-02 15:15:59 +08:00
|
|
|
|
await this.FileListChanged.InvokeAsync(this.FileList);
|
|
|
|
|
|
2020-07-21 00:16:02 +08:00
|
|
|
|
await InvokeAsync(StateHasChanged);
|
2020-09-09 00:52:14 +08:00
|
|
|
|
await JSRuntime.InvokeVoidAsync(JSInteropConstants.UploadFile, _file, index, Data, Headers, id, Action, Name, _currentInstance, "UploadChanged", "UploadSuccess", "UploadError");
|
2020-07-21 00:16:02 +08:00
|
|
|
|
index++;
|
2020-07-01 06:49:51 +08:00
|
|
|
|
}
|
2020-07-21 00:16:02 +08:00
|
|
|
|
|
2020-09-09 00:52:14 +08:00
|
|
|
|
await JSRuntime.InvokeVoidAsync(JSInteropConstants.ClearFile, _file);
|
2020-07-01 06:49:51 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-07-29 11:26:20 +08:00
|
|
|
|
private async Task RemoveFile(UploadFileItem item)
|
|
|
|
|
{
|
|
|
|
|
var canRemove = OnRemove == null || await OnRemove?.Invoke(item);
|
|
|
|
|
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();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-01 06:49:51 +08:00
|
|
|
|
[JSInvokable]
|
|
|
|
|
public async Task UploadSuccess(string id, string returnData)
|
|
|
|
|
{
|
|
|
|
|
var file = FileList.FirstOrDefault(x => x.Id.Equals(id));
|
|
|
|
|
if (file == null)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
file.State = UploadState.Success;
|
|
|
|
|
file.Progress = 100;
|
|
|
|
|
file.Response = returnData;
|
|
|
|
|
_uploadInfo.File = file;
|
2020-07-21 00:16:02 +08:00
|
|
|
|
await UploadChanged(id, 100);
|
2020-07-01 06:49:51 +08:00
|
|
|
|
await InvokeAsync(StateHasChanged);
|
|
|
|
|
if (OnSingleCompleted.HasDelegate)
|
|
|
|
|
{
|
|
|
|
|
await OnSingleCompleted.InvokeAsync(_uploadInfo);
|
|
|
|
|
}
|
|
|
|
|
if (OnCompleted.HasDelegate && FileList.All(x => (x.State.Equals(UploadState.Success) || x.State.Equals(UploadState.Fail))))
|
|
|
|
|
{
|
|
|
|
|
await OnCompleted.InvokeAsync(_uploadInfo);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[JSInvokable]
|
|
|
|
|
public async Task UploadError(string id, string reponseCode)
|
|
|
|
|
{
|
|
|
|
|
var file = FileList.FirstOrDefault(x => x.Id.Equals(id));
|
|
|
|
|
if (file == null)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
file.State = UploadState.Fail;
|
|
|
|
|
file.Progress = 100;
|
|
|
|
|
_uploadInfo.File = file;
|
|
|
|
|
file.Response ??= "error";
|
2020-07-21 00:16:02 +08:00
|
|
|
|
await UploadChanged(id, 100);
|
2020-07-01 06:49:51 +08:00
|
|
|
|
await InvokeAsync(StateHasChanged);
|
|
|
|
|
if (OnSingleCompleted.HasDelegate)
|
|
|
|
|
{
|
|
|
|
|
await OnSingleCompleted.InvokeAsync(_uploadInfo);
|
|
|
|
|
}
|
|
|
|
|
if (OnCompleted.HasDelegate && FileList.All(x => (x.State.Equals(UploadState.Success) || x.State.Equals(UploadState.Fail))))
|
|
|
|
|
{
|
|
|
|
|
await OnCompleted.InvokeAsync(_uploadInfo);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[JSInvokable]
|
|
|
|
|
public async Task UploadChanged(string id, int progress)
|
|
|
|
|
{
|
|
|
|
|
var file = FileList.FirstOrDefault(x => x.Id.Equals(id));
|
|
|
|
|
if (file == null)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
file.Progress = progress;
|
2020-07-21 00:16:02 +08:00
|
|
|
|
_uploadInfo.File = file;
|
2020-07-01 06:49:51 +08:00
|
|
|
|
await InvokeAsync(StateHasChanged);
|
|
|
|
|
if (OnChange.HasDelegate)
|
|
|
|
|
{
|
|
|
|
|
await OnChange.InvokeAsync(_uploadInfo);
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-07-31 00:46:46 +08:00
|
|
|
|
|
2020-07-21 00:16:02 +08:00
|
|
|
|
protected override void Dispose(bool disposing)
|
|
|
|
|
{
|
2020-09-09 00:52:14 +08:00
|
|
|
|
InvokeAsync(async () => await JSRuntime.InvokeVoidAsync(JSInteropConstants.RemoveFileClickEventListener, _btn));
|
2020-07-21 00:16:02 +08:00
|
|
|
|
|
|
|
|
|
base.Dispose(disposing);
|
|
|
|
|
}
|
2020-07-01 06:49:51 +08:00
|
|
|
|
}
|
|
|
|
|
}
|