How To: Deleting Temp Files For C1Upload

C1Upload control for ASP.Net is an advanced control which lets you upload files on the server. Multiple files can be uploaded at a time. While file uploading is in progress, users can also cancel the uploading of the files. User can either can cancel uploading all of the files using the CancelAll button or may choose to cancel any particular file using the Cancel button corresponding it. When the progress is cancelled file is not uploaded to the server. However, files may be present in the Temp folder and takes up space on the server. This blog demonstrates how you can delete the files present in the temp folder after uploading of file is cancelled. Client side click events for Cancel button and CancelAll button are handled to pass an AJAX call to a server side page method. In the server side method, temp files and directories are deleted. Following code implementation shows the handling of click events for Cancel and CancelAll buttons:

<script type="text/javascript">  
 $(document).ready(function () {  
   var count = -1;  
    //cancel buuttons are shown when progress event is raised.  
    progress: function (e) {  
      //click event of Cancel Button  
      $(".wijmo-wijupload-cancel").click(function () {  
         //condition to restrict calling server side method only once.  
          if (count == 0) {  
  //click event of CancelAll button  
  $(".wijmo-wijupload-cancelAll").click(function () {  
//Function to make an ajax() call to server side method.  
function DeleteTempFiles() {  
  //place an ajax call to method "DeleteTempFiles"  
  //for deleting files in Temp folder  
         type: "POST",  
         url: "Default.aspx/DeleteTempFiles",  
         data: "{}",  
         contentType: "application/json",  
         charset: "utf-8",  
         dataType: "json",  
         async: true,  
         cache: false,  
         success: function (msg) {  
            //message returned from server side method.  
         error: function (x, e) {  
            alert("The call to the server side failed. " + x.responseText);  

The ajax() call raises a server side WebMethod which deletes all the temp files from the folder name specified in the TempFolder property of C1Upload. Following code snippet implements the server side method used to delete the temp files.

protected void Page_Load(object sender, EventArgs e)  
{   //save path of TempFolder in Session variable  
    Session["Path"] = C1Upload1.TempFolder;  
public static string DeleteTempFiles()  
  try {  
    //Get path of TempFolder from Session object  
    string Path=HttpContext.Current.Session["Path"].ToString();  
    System.IO.DirectoryInfo TempFolder = new DirectoryInfo(HttpContext.Current.Server.MapPath(Path));  
    foreach (FileInfo file in TempFolder.GetFiles())  
       file.Open(FileMode.Open, FileAccess.ReadWrite);  
   catch(Exception ex){  
   return "temp files deleted";  

Thats it..!!! Temp Files are deleted and it saves the extra space occupied by these temp files on the server. :) Download Sample for complete implementation.


GrapeCity Developer Tools
comments powered by Disqus