ASP.NET method to bulk download files

  • 2021-01-22 04:59:42
  • OfStack

This article illustrates the ASP.NET method for bulk downloading files. Share with you for your reference. The specific methods are as follows:

1. Implementation steps

In the user interface, the user selects the files to be downloaded. According to the selected files, the system creates a temporary folder for storing the selected files on the server and copies the selected files to the temporary folder. The RAR program is then called to compress the temporary folder and output it to the client. Finally, delete the temporary folder.

2. Code implementation

1, ASP.NET batch download core code

// Iterate through all files in the folder specified by the server 
string path = "uploads/Image/";
string serverPath = Server.MapPath(path);
// Create a temporary folder
string tempName = DateTime.Now.ToString("yyyyMMddHHMMss");
string tempFolder = Path.Combine(serverPath, tempName);
Directory.CreateDirectory(tempFolder);
DirectoryInfo folder = new DirectoryInfo(serverPath);
foreach (FileInfo file in folder.GetFiles())
{
 string filename = file.Name;
 File.Copy(serverPath + "/" + filename, tempFolder + "/" + filename);
}
//ZKHelper.JSHelper.Alert(" The picture was copied successfully. !");
// produce RAR File, and file output
RARSave(tempFolder, tempName);
DownloadRAR(tempFolder + "\\\\" + tempName + ".rar");


2, RARSave(string tempFolder, string tempName) method

/// <summary>
/// generate RAR file
/// </summary>
/// <param name="path"> The directory where the copied files are stored </param>
/// <param name="rarPatch">RAR File directory </param>
/// <param name="rarName">RAR The file name </param>
private void RARSave(string rarPatch, string rarName)
{
    string the_rar;
    RegistryKey the_Reg;
    Object the_Obj;
    string the_Info;
    ProcessStartInfo the_StartInfo;
    Process the_Process;
    try
    {
 the_Reg = Registry.ClassesRoot.OpenSubKey(@"WinRAR");
 the_Obj = the_Reg.GetValue("");
 the_rar = the_Obj.ToString();
 the_Reg.Close();
 the_rar = the_rar.Substring(1, the_rar.Length - 7);
 the_Info = " a " + rarName + " -r";
 the_StartInfo = new ProcessStartInfo();
 the_StartInfo.FileName = "WinRar";//the_rar;
 the_StartInfo.Arguments = the_Info;
 the_StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
 // The directory where the files are packaged
 the_StartInfo.WorkingDirectory = rarPatch;
 the_Process = new Process();
 the_Process.StartInfo = the_StartInfo;
 the_Process.Start();
 the_Process.WaitForExit();
 the_Process.Close();
    }
    catch (Exception)
    {
 throw;
    }
}


3, DownloadRAR(string file) method

/// <summary>
/// download-generated RAR file
/// </summary>
private void DownloadRAR(string file)
{
    FileInfo fileInfo = new FileInfo(file);
    Response.Clear();
    Response.ClearContent();
    Response.ClearHeaders();
    Response.AddHeader("Content-Disposition", "attachment;filename=" + fileInfo.Name);
    Response.AddHeader("Content-Length", fileInfo.Length.ToString());
    Response.AddHeader("Content-Transfer-Encoding", "binary");
    Response.ContentType = "application/octet-stream";
    Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
    Response.WriteFile(fileInfo.FullName);
    Response.Flush();
    string tempPath = file.Substring(0, file.LastIndexOf("\\\\"));
    // Delete all files in the temporary directory
    DeleteFiles(tempPath);
    // Delete empty directory
    Directory.Delete(tempPath);
    Response.End();
}

4, DeleteFiles(string tempPath) method

/// <summary>
/// Delete all files in the temporary directory
/// </summary>
/// <param name="tempPath"> Temporary directory path </param>
private void DeleteFiles(string tempPath)
{
    DirectoryInfo directory = new DirectoryInfo(tempPath);
    try
    {
 foreach (FileInfo file in directory.GetFiles())
 {
     if (file.Attributes.ToString().IndexOf("ReadOnly") != -1)
     {
  file.Attributes = FileAttributes.Normal;
     }
     File.Delete(file.FullName);
 }
    }
    catch (Exception)
    {
 throw;
    }
}

I hope this article is helpful to your asp.net# program design.


Related articles: