How to delete directory

Hello,

I am uploading a directory in which I have more than 5 10+ or more images. After Successfully inserting details of images into the database how to delete the same directory and its images from my project. 


<ejs-uploader id="postedImages" selected="onSelect" asyncSettings="@asyncSettingsImages" directoryUpload="true" success="onSuccessImages"></ejs-uploader>

<ejs-button id="dialogBtn" content="Save"></ejs-button>

var asyncSettingsImages = new Syncfusion.EJ2.Inputs.UploaderAsyncSettings { SaveUrl = @Url.Content("~/UploadImages/Save/") };


public void Save( IList<IFormFile> postedImages )

{

HttpContext.Session.SetString("ImageUploadDirPath", "");

long size = 0;

try

{

foreach (var file in postedImages )

{

var filename = ContentDispositionHeaderValue

.Parse(file.ContentDisposition)

.FileName

.Trim('"');

var folders = filename.Split('/');

var uploaderFilePath = hostingEnv.WebRootPath;

// for Directory upload

if (folders.Length > 1)

{

for (var i = 0; i < folders.Length - 1; i++)

{

var newFolder = uploaderFilePath + $@"\{folders[i]}";

Directory.CreateDirectory(newFolder);

uploaderFilePath = newFolder;

filename = folders[i + 1];

}

}

HttpContext.Session.SetString("ImageUploadDirPath", uploaderFilePath);

filename = uploaderFilePath + $@"\{filename}";

size += file.Length;

if (!System.IO.File.Exists(filename))

{

using (FileStream fs = System.IO.File.Create(filename))

{

file.CopyTo(fs);

fs.Flush();

}

}

//Call my DATABASE method for inserting records and uploading my files into my Azure


}

}

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;

}

}


Please Check the comment line where I am interested to remove the directory + its files from my project. 

//Call my DATABASE method for inserting records and uploading my files into my Azure.

In the next line, I am interested to remove the uploaded directory. 

my session variable has directory details 

HttpContext.Session.SetString("ImageUploadDirPath", "");


1 Reply 1 reply marked as answer

PM Ponmani Murugaiyan Syncfusion Team February 28, 2022 03:40 PM UTC

Hi Veet, 

Query: how to delete the same directory and its images from my project. 

You can remove the uploaded file using the server-side configuration for remove actions. For more information, please find the below documentation link.  
 
 
var asyncSettings = new Syncfusion.EJ2.Inputs.UploaderAsyncSettings { SaveUrl = "api/Values/Save", RemoveUrl = "api/Values/Remove" }; 
 
public IActionResult Remove(IList<IFormFile> UploadFiles) 
        { 
            try 
            { 
                foreach (var file in UploadFiles) 
                { 
                    var fileName = ContentDispositionHeaderValue.Parse(file.ContentDisposition).FileName.Trim('"'); 
                    var filePath = Path.Combine(hostingEnv.WebRootPath); 
                    var fileSavePath = filePath + "\\" + fileName; 
                    if (!System.IO.File.Exists(fileSavePath)) 
                    { 
                        System.IO.File.Delete(fileSavePath); 
                    } 
                } 
            } 
            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; 
            } 
            return Content(""); 
        } 

Regards, 
Ponmani M 


Marked as answer
Loader.
Up arrow icon