We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

Uploader rename file

Hello,

I would like to rename the file uploaded with EjsUploader but I can't do it neither before nor after uploading, I tried the following two ways:

1) Renaming the file server-side when it's uploaded but the client doesn't know the new name, so the remove function of Uploader can't work anymore

2) Sending the new name for the file as additional parameter doesn't work or I'm doing something not correct as it is not documented:

<EjsUploader @ref="_uploader" ID="uploadFiles" AllowedExtensions=".pdf" Multiple="false">
     <UploaderEvents BeforeUpload="RenameFile" />
     <UploaderAsyncSettings SaveUrl="uploader/upload" RemoveUrl="/remove" />
</EjsUploader>

private void RenameFile(BeforeUploadEventArgs e)
{
     e.CustomFormData = // Send the new file name, what object should I make?
}

[HttpPost("[action]")]
public void Upload(IList<IFormFile> uploadFiles)
{
     ...
}


I'd like it if someone could help me, thank you.

1 Reply

SP Sureshkumar P Syncfusion Team October 8, 2019 05:03 AM

Hi Fabrizio, 

Greetings from Syncfusion support. 

Sure, before uploading and deleting, we can rename the file by passing the custom parameter in the event FileSelected and BeforeRemove. We created and attached the sample based on your requirement.  

Kindly refer to the code block. 
[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; 
            } 
        } 
    } 



Regards, 
Sureshkumar P

Loader.
Live Chat Icon For mobile
Up arrow icon