C1Upload 控件可以用于上传文件到服务器,可以一次性上传多个文件。当文件上传时,用户可以取消上传。同时,可以通过“取消全部”按钮取消所有上传动作。或者通过,“停止”按钮取消特定文件的上传。当上传动作被取消时,文件将停止上传至服务器。

但是,文件可能存在于Temp Folder”中,占用服务器空间。本篇文字将讲述如何删除“Temp Folder”中的临时文件。

客户端“取消”和“取消全部”按钮 click 事件会执行 Ajax 异步调用。在服务器端的方法中,我们可以删除临时文件。以下代码实为前台 Ajax 方法:

<script type="text/javascript">
 $(document).ready(function () {
   var count = -1;
   $("#C1Upload1").c1upload({
    //cancel buuttons are shown when progress event is raised.
    progress: function (e) {
      //click event of Cancel Button
      $(".wijmo-wijupload-cancel").click(function () {
         count++;
         //condition to restrict calling server side method only once.
          if (count == 0) {
            DeleteTempFiles();
          }
       });
    }
   });
  //click event of CancelAll button
  $(".wijmo-wijupload-cancelAll").click(function () {
     DeleteTempFiles();
  });
});
//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
  $.ajax({
         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.
            alert(msg.d);
         },
         error: function (x, e) {
            alert("The call to the server side failed. " + x.responseText);
         }
     });
}
</script>
ajax 调用后台 WebMethod 方法删除 TempFolder 属性所对应的临时文件夹。代码如下:
protected void Page_Load(object sender, EventArgs e)
{   //save path of TempFolder in Session variable
    Session["Path"] = C1Upload1.TempFolder;
}
[WebMethod]
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);
       file.Delete();
    }
   }
   catch(Exception ex){
   }
   return "temp files deleted";
 
下载示例:Demo