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. (Last updated on: June 24, 2019).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

Uploader rename file

Thread ID:

Created:

Updated:

Platform:

Replies:

148133 Oct 7,2019 05:16 PM UTC Oct 8,2019 10:03 AM UTC Blazor 1
loading
Tags: File Upload
Fabrizio Alessandro
Asked On October 7, 2019 05:16 PM UTC

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.

Sureshkumar P [Syncfusion]
Replied On October 8, 2019 10:03 AM UTC

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

CONFIRMATION

This post will be permanently deleted. Are you sure you want to continue?

Sorry, An error occured while processing your request. Please try again later.

Please sign in to access our forum

This page will automatically be redirected to the sign-in page in 10 seconds.

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile
Live Chat Icon