[index.razor]
@using Syncfusion.EJ2.Blazor
@using Syncfusion.EJ2.Blazor.Inputs
<EjsUploader @ref="uploadObj" ID="UploadFiles" Multiple="false" AutoUpload="true">
<UploaderAsyncSettings SaveUrl="api/Default/Save" RemoveUrl="api/Default/Remove"></UploaderAsyncSettings>
<UploaderEvents FileSelected="@Selected" BeforeRemove="@Remove"></UploaderEvents>
</EjsUploader>
@code{
EjsUploader uploadObj;
FileInfo fileModel = new FileInfo();
void Selected(SelectedEventArgs args)
{
// custom parameter in the selected event
args.CurrentRequest = new List<object> { new { FileName = "Testing" } };
}
void Remove(BeforeRemoveEventArgs args)
{
// custom parameter in the before remove event
args.CurrentRequest = new List<object> { new { FileName = "Testing" } };
}
}
[DefaultController.cs]
[HttpPost("[action]")]
public void Save(IList<IFormFile> UploadFiles)
{
//getting filename using custom parameter
var FileName = Response.HttpContext.Request.Headers["FileName"].ToString();
long size = 0;
try
{
foreach (var file in UploadFiles)
{
var filename = ContentDispositionHeaderValue
.Parse(file.ContentDisposition)
.FileName
.Trim('"');
filename = hostingEnv.ContentRootPath + $@"\{FileName}";
size += (int)file.Length;
if (!System.IO.File.Exists(filename))
{
using (FileStream fs = System.IO.File.Create(filename))
{
file.CopyTo(fs);
fs.Flush();
}
}
}
}
catch (Exception e)
{
Response.Clear();
Response.StatusCode = 204;
Response.HttpContext.Features.Get<IHttpResponseFeature>().ReasonPhrase = "File failed to upload";
Response.HttpContext.Features.Get<IHttpResponseFeature>().ReasonPhrase = e.Message;
}
}
[HttpPost("[action]")]
public void Remove(IList<IFormFile> UploadFiles)
{
try
{
//getting filename using custom parameter
var FileName = Response.HttpContext.Request.Headers["FileName"].ToString();
var filename = hostingEnv.ContentRootPath + $@"\{FileName}";
if (System.IO.File.Exists(filename))
{
System.IO.File.Delete(filename);
}
}
catch (Exception e)
{
Response.Clear();
Response.StatusCode = 200;
Response.HttpContext.Features.Get<IHttpResponseFeature>().ReasonPhrase = "File removed successfully";
Response.HttpContext.Features.Get<IHttpResponseFeature>().ReasonPhrase = e.Message;
}
}
} |
Hi Support ,
If I use Blazor SfUploader compoment, what is the best practice to rename upload file ?
Regards!
Jacky
Hi Jacky,
As per our previous update, you can rename the file before uploading or before deleting the file using the FileSelected and BeforeRemove events.
Note: If this post is helpful, please consider Accepting it as the solution so that other members can locate it more quickly.
Regards,
Sureshkumar P
Hi Sureshkumar ,
I use the rename by the FileSelected and BeforeRemove events trigger is ok.
Above sample is controller to use Response.HttpContext.Request.
But what is the best practice to pass the rename to OnChange(UploadChangeEventArgs args) for write rename file ?
My Sample as Attach file.
Best Regards!
Jacky
Hi Jacky,
We have passed the additional data (rename files) as a header when using the external API call. When we pass the rename to the OnChange event we can directly pass the rename string value using the variable.
Find the code example here:
@code { private SfUploader uploadObj { get; set; } private bool SetAutoUpload { get; set; } = false; private bool SetSequentialUpload { get; set; } = false;
public string FileRename { get; set; } public string _uploadfilepath;
protected override void OnInitialized() { //SaveUrl = $"{configuration.GetValue<string>("AppStrings:ServerLinkRoute")}/api/uploadbox/Save"; //RemoveUrl = $"{configuration.GetValue<string>("AppStrings:ServerLinkRoute")}/api/uploadbox/Remove"; _uploadfilepath = @"path"; }
public void OnFileRemove(RemovingEventArgs args) { args.PostRawFile = false; } public void OnFileBeforeRemove(BeforeRemoveEventArgs args) { FileRename = "Testing"; } public void OnFileSelected(SelectedEventArgs args) { FileRename = "Testing"; } public void OnAutoChange(Syncfusion.Blazor.Buttons.ChangeEventArgs<bool> args) { this.uploadObj.ClearAllAsync(); this.SetAutoUpload = args.Checked; } public void OnSequentialChange(Syncfusion.Blazor.Buttons.ChangeEventArgs<bool> args) { this.uploadObj.ClearAllAsync(); this.SetSequentialUpload = args.Checked; } private async Task OnChange(UploadChangeEventArgs args) { await fileUpload.UploadSfu(args.Files, "BZSF10", FileRename); }
} |
Find the modified sample in the attachment:
Note: If this post is helpful, please consider Accepting it as the solution so that other members can locate it more quickly.
Regards,
Sureshkumar P