DevExtreme v26.1 is now available.

Explore our newest features/capabilities and share your thoughts with us.

Your search did not match any results.

Angular File Uploader - Custom Drop Zone

The FileUploader component supports the drag-and-drop functionality to upload files.

In this demo, the dropZone property specifies the HTML element in which you can drag and drop files for upload. The dropZoneEnter and dropZoneLeave events allow you to customize the drop zone when the mouse pointer enters and leaves this zone while dragging the file.

Backend API
<div class="widget-container flex-box"> <span>Profile Picture</span> <div id="dropzone-external" class="flex-box" [class]="isDropZoneActive ? 'dropzone-active' : null" > @if (imageSource) { <img id="dropzone-image" [src]="imageSource" alt="" /> } @if (textVisible) { <div id="dropzone-text" class="flex-box"> <span>Drag & Drop the desired file</span> <span>…or click to browse for a file instead.</span> </div> } <dx-progress-bar #uploadProgress [min]="0" [max]="100" width="30%" [showStatus]="false" [visible]="progressVisible" [value]="progressValue" ></dx-progress-bar> </div> <dx-file-uploader #fileUploader dialogTrigger="#dropzone-external" dropZone="#dropzone-external" [multiple]="false" [allowedFileExtensions]="allowedFileExtensions" uploadMode="instantly" uploadUrl="https://js.devexpress.com/Demos/NetCore/FileUploader/Upload" [visible]="false" (onDropZoneEnter)="onDropZoneEnter($event)" (onDropZoneLeave)="onDropZoneLeave($event)" (onUploaded)="onUploaded($event)" (onProgress)="onProgress($event)" (onUploadStarted)="onUploadStarted()" ></dx-file-uploader> </div>
import { bootstrapApplication } from '@angular/platform-browser'; import { ChangeDetectorRef, Component, enableProdMode, provideZoneChangeDetection, } from '@angular/core'; import { DxFileUploaderModule, DxProgressBarModule } from 'devextreme-angular'; import 'anti-forgery'; if (!/localhost/.test(document.location.host)) { enableProdMode(); } @Component({ selector: 'demo-app', templateUrl: './app.component.html', styleUrls: ['./app.component.css'], imports: [ DxFileUploaderModule, DxProgressBarModule, ], }) export class AppComponent { isDropZoneActive = false; imageSource = ''; textVisible = true; progressVisible = false; progressValue = 0; allowedFileExtensions: string[] = ['.jpg', '.jpeg', '.gif', '.png']; constructor(private changeDetectorRef: ChangeDetectorRef) { this.onDropZoneEnter = this.onDropZoneEnter.bind(this); this.onDropZoneLeave = this.onDropZoneLeave.bind(this); this.onUploaded = this.onUploaded.bind(this); this.onProgress = this.onProgress.bind(this); this.onUploadStarted = this.onUploadStarted.bind(this); } onDropZoneEnter({ component, dropZoneElement, event }) { if (dropZoneElement.id === 'dropzone-external') { const items = event.originalEvent.dataTransfer.items; const allowedFileExtensions = component.option('allowedFileExtensions'); const draggedFileExtension = `.${items[0].type.replace(/^image\//, '')}`; const isSingleFileDragged = items.length === 1; const isValidFileExtension = allowedFileExtensions.includes(draggedFileExtension); if (isSingleFileDragged && isValidFileExtension) { this.isDropZoneActive = true; } } } onDropZoneLeave(e) { if (e.dropZoneElement.id === 'dropzone-external') { this.isDropZoneActive = false; } } onUploaded(e) { const file = e.file; const fileReader = new FileReader(); fileReader.onload = () => { this.isDropZoneActive = false; this.imageSource = fileReader.result as string; this.changeDetectorRef.detectChanges(); }; fileReader.readAsDataURL(file); this.textVisible = false; this.progressVisible = false; this.progressValue = 0; } onProgress(e) { this.progressValue = e.bytesLoaded / e.bytesTotal * 100; } onUploadStarted() { this.imageSource = ''; this.progressVisible = true; } } bootstrapApplication(AppComponent, { providers: [ provideZoneChangeDetection({ eventCoalescing: true, runCoalescing: true }), ], });
::ng-deep #dropzone-external { width: 350px; height: 350px; background-color: rgba(0,0,0,0.08); border-width: 2px; border-style: dashed; padding: 10px; border-color: var(--dx-color-border); } ::ng-deep #dropzone-external > * { pointer-events: none; } ::ng-deep #dropzone-external.dropzone-active { border-style: solid; border-color: var(--dx-color-primary); } ::ng-deep .widget-container > span { font-size: 22px; font-weight: bold; margin-bottom: 16px; } ::ng-deep #dropzone-image { max-width: 100%; max-height: 100%; } ::ng-deep #upload-progress { display: flex; margin-top: 10px; } ::ng-deep .flex-box { display: flex; flex-direction: column; justify-content: center; align-items: center; }
<!DOCTYPE html> <html lang="en"> <head> <title>DevExtreme Demo</title> <meta http-equiv="X-UA-Compatible" content="IE=edge" /> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=5.0" /> <link rel="stylesheet" type="text/css" href="https://cdn3.devexpress.com/jslib/26.1.5/css/dx.light.css" /> </head> <body class="dx-viewport"> <div class="demo-container"> <demo-app>Loading...</demo-app> </div> <script src="https://cdn.jsdelivr.net/npm/zone.js@0.16.2/bundles/zone.umd.js"></script> <script src="./bundle.js" type="module"></script> </body> </html>

Allowed file extensions: .jpg, .jpeg, .gif, .png

Maximum file size: 4 MB