Compressing files and downloading

Home > Basic examples > Example 12

Explanation

DEXTUploadX5 supports compressed download since version 1.3.0.0.

In order to receive multiple files at the same time, the compression download requests the server to bundle the target file into one compressed file on the server, and downloads the compressed file. Unlike the single/multiple download example we saw in the other examples, the compressed download does not affect the downUrl property, which specifies the download path to the target file, because it compresses the process to the server.

To perform a compressed download, you must specify the address that will handle the compression, and return the path value to download the compressed file using the setCompressURL method. Unlike single/multiple download, the downloadCompressed method is used when you start a compressed download.

var dx = dx5.get("component id");
// The downUrl property is unnecessary.
// You must specify the value of the vindex property to distinguish targets on the server.
// vindex is a unique string value, which is the only way to distinguish between virtual files. 
dx.addVirtualFile({ vindex: "IDX0003", name: "bridge_509147.jpg", size: 509147 });
dx.addVirtualFile({ vindex: "IDX0004", name: "beach_239826.jpg", size: 239826 });
dx.addVirtualFile({ vindex: "IDX0005", name: "cosmos (empty) 195779.jpg", size: 195779 });

// Set the address to process the compression and return the compressed file download path. 
dx.setCompressURL("http://domain/path../service/compress.ashx");

// Perform the download according to the flag value.
// AUTO: Download compressed virtual files.
// SELECTED: Download compressed selected virtual files.
// CHECKED: Download compressed checked virtual files. 
dx.downloadCompressed("SELECTED");

File compression and download are handled by the "compress.ashx." DEXTUploadX5 sends the form data with the name 'DEXTUploadX5_VIndexes' to the server in POST method when the downloadCompressed method is called, and the vindex property value of the virtual file to be downloaded is a list with a comma (,) as a separator.

// You can get a set of virtual indexes to compress from the "DEXTUploadX5_VIndexes" form name.
var data = context.Request.Form["DEXTUploadX5_VIndexes"] ?? string.Empty;
var vindexes = data.Split(',');
var targets = new List<string>();

foreach (string index in vindexes)
{
    if (index.Equals("IDX0003")) targets.Add(context.Server.MapPath("~/files/attach/bridge_509147.jpg"));
    else if (index.Equals("IDX0004")) targets.Add(context.Server.MapPath("~/files/attach/beach_239826.jpg"));
    else if (index.Equals("IDX0005")) targets.Add(context.Server.MapPath("~/files/attach/cosmos (empty) 195779.jpg"));
}

var zipPath = Path.GetTempFileName();

// Create compressed file using ZipArchive supported from .NET Framework 4.5.
using (var fs = new FileStream(zipPath, FileMode.Create))
using (var za = new ZipArchive(fs, ZipArchiveMode.Create, false, System.Text.Encoding.UTF8))
{
    foreach (string path in targets)
    {
        za.CreateEntryFromFile(path, Path.GetFileName(path));
    }
}

// In a real situation other than an example, you should use a different storage method, such as a session or a database,
// so that you can get the path to the compressed file in a handler (zip-download.ashx) that actually needs to download.
...

// Create a handler path to download the compressed file.
UriBuilder b = new UriBuilder(context.Request.Url);
b.Path = VirtualPathUtility.ToAbsolute("~/service/zip-download.ashx");
b.Query = string.Concat("key=", key);

// The path to download the compressed file should be delivered to the client in the response data. 
context.Response.ContentType = "text/plain";
context.Response.Write(b.ToString());
var key = context.Request.QueryString["key"] ?? string.Empty;
var entry = FileRepository.Get(key);

if (entry != null)
{
    using (var dext = new DEXTUpload.NET.FileDownload())
    {
        dext.Download(entry.Path, entry.FileName, new DEXTUpload.NET.DownloadOption
        {
            // After downloading, the compressed file is meaningless, so delete it
            RemoveAfterDownloading = true,
            // The compressed file is created for every request, so the client cache Do not use it.
            UseClientCache = false,
            // application/x-zip-compressed
            MimeType = entry.MimeType
        });
    }
}
else
{
    throw new HttpException(404, "The zip file not found.");
}

Compressing files is a task that consumes a lot of server resources (CPU, I/O), so if you have a large number of files that are large files or compressed files, depending on the situation, it may take a long time to wait for the file compression process, which may lead to a problem of a session being disconnected or a serious problem that the service itself is stopped. Therefore, it is recommended to use the function after establishing policy restrictions for compressed download.

The DEXTUpload.NET Professional product does not provide libraries such as classes for compressing tasks. In the .NET Framework 4.5 and later, compression-related classes such as ZipArchive and ZipFile are provided so that you can use them to compress files. If you have an older version, such as the .NET Framework 4.0, you must find a way to use third-party libraries or develop on your own.

Example

Compress download

Compression proceeds on the server. The progress window is displayed while the compression is in progress, but when the compressed file is downloaded, a single file download is performed. Therefore, the progress window for the downloading process is not provided.