DropZone
Wisej.NET 4.2.0+
A DropZone is an upload area with a queue: users drop files on it, or click it (or press Enter on it) to browse, and every file gets a row of its own with its type, its name, a status line and a progress bar. Files are checked against your rules as soon as they are chosen, uploaded one after the other, and handed to your code one at a time.
The Upload control is a button that sends what the user picked in one request and raises Uploaded with the whole batch. Reach for the DropZone instead when the upload is a step of its own on the page: importing spreadsheets, attaching documents to a ticket, collecting scans. It adds the drop target, per-file validation and progress, a manual mode where nothing is sent until the user confirms, cancel and retry, and a list the user can take files out of. Both use the same Wisej upload transport.
For a complete list of properties, methods, and events, see the API documentation.
Features
Validation Rules
AllowedExtensions lists the accepted extensions, such as .csv; matching ignores case and an empty array accepts everything. MaxFileSize limits each file in bytes and MaxFiles limits the number of entries in the queue, completed files included, until they are removed or cleared. Zero means no limit for both. Multiple decides whether more than one file can be chosen at a time.
Every file is checked twice: when it is chosen, against its name and size, and again on the server against the request that actually arrives, so a tampered request cannot slip past the first check. A file that fails either check stays in the list with its reason and raises ValidationFailed. Empty files are always rejected, because the upload transport skips them.
An extension check is not content inspection: a file named .csv can contain anything. Validate the content when you read it, and remember that the server's request size limits apply on top of MaxFileSize.
The Upload Queue
Accepted files are uploaded one at a time, in the order they were added. Each file goes through its statuses in the list (validating, queued, uploading, completed, failed or canceled) and raises its events along the way:
| Event | When |
|---|---|
FilesAdded | Once for each file accepted into the queue. |
UploadProgress | As the browser reports progress; Loaded is the number of bytes sent. |
UploadCompleted | After the server has received and validated the file. UploadedFile holds it, and only during this event. |
UploadFailed | When an upload fails or is canceled; Message says why. |
ValidationFailed | When a file or the received upload breaks a rule; Message says which. |
FileRemoved | When the user removes a file from the list. |
FileClick | When the user clicks an accepted file, or presses Enter or Space on it. |
All of them carry a DropZoneFileEventArgs. Its File is a DropZoneFile with an Id, unique in the queue even when two files share a name, the Name the browser reported and the Size in bytes. The name is a display name, never a server path.
Automatic and Manual Uploads
With UploadMode set to DropZoneUploadMode.Auto, the default, a file starts uploading as soon as the server has approved it. With Manual the files wait in the queue until you call Upload(), typically from an Import button, so the user can review the list and remove files first. CancelUpload() aborts the current file and pauses the rest; Upload() resumes them. Clear() aborts whatever is running and empties the list.
Layout and Text
Compact turns the surface into a single row for tight forms, and ShowFileList hides the list when you show progress your own way. HintText is the main prompt ("Drop files here or browse") and SubHintText the line under it, the natural place to state the rules. Every status and validation message has a property of its own, from FileTooLargeText to UploadingText; leave them null to use the built-in translation for the current culture, or set an empty string to hide a message.
How To
Save the Files That Arrive
Nothing is saved for you. Read or copy the stream in UploadCompleted, synchronously, and keep the File.Id if you need to find the file again later, for example in FileClick.
private void dropZone1_UploadCompleted(object sender, DropZoneFileEventArgs e)
{
var target = Path.Combine(this.importFolder, e.File.Id + Path.GetExtension(e.File.Name));
using (var output = File.Create(target))
e.UploadedFile.InputStream.CopyTo(output);
this.imports.Add(new ImportFile { Id = e.File.Id, Name = e.File.Name, Path = target });
}
Collect Files and Import on Demand
this.dropZone1.AllowedExtensions = new[] { ".csv", ".xlsx" };
this.dropZone1.MaxFileSize = 25 * 1024 * 1024;
this.dropZone1.MaxFiles = 5;
this.dropZone1.Multiple = true;
this.dropZone1.UploadMode = DropZoneUploadMode.Manual;
this.dropZone1.SubHintText = "CSV or Excel, up to 25 MB each, 5 files at most";
this.importButton.Click += (s, e) => this.dropZone1.Upload();
this.cancelButton.Click += (s, e) => this.dropZone1.CancelUpload();
Report Problems to the User
this.dropZone1.ValidationFailed += (s, e) =>
// The file name comes from the browser: show it as text, not HTML.
AlertBox.Show($"{e.File.Name}: {e.Message}", MessageBoxIcon.Warning, allowHtml: false);
this.dropZone1.UploadFailed += (s, e) =>
this.log.Warn($"Upload of {e.File.Name} failed after {e.Loaded} of {e.File.Size} bytes: {e.Message}");
this.dropZone1.FileRemoved += (s, e) =>
this.imports.RemoveAll(f => f.Id == e.File.Id);
Removing a file from the list does not delete the copy your code saved; FileRemoved is where to do that.
Advanced
JavaScript Widget
| Item | Description |
|---|---|
| Class name | "wisej.web.DropZone" |
| Theme appearance | "dropzone", and "dropzone-row", "dropzone-filetype", "dropzone-message", "dropzone-progress" and "dropzone-remove" for the parts of a file row. See Themes. |
| Child components | "surface" is the drop target, "browse" the prompt that opens the file dialog, "hint" the secondary hint, and "list" the file list. See JavaScript. |