Angular FileUploader - Specify a file's GUID

Use the valueChanged event to generate a unique identifier (GUID) for a file before it is uploaded and pass it as a parameter to the upload URL.

View on GitHub

app.component.html
app.component.ts
  • <dx-file-uploader
  • [(uploadUrl)]="uploadUrl"
  • [multiple]="false"
  • uploadMode="instantly"
  • (onValueChanged)="onValueChanged($event)">
  • </dx-file-uploader>
  • import { Component, NgModule } from '@angular/core';
  • import { BrowserModule } from "@angular/platform-browser";
  • import { DxFileUploaderModule } from "devextreme-angular";
  •  
  • @Component({
  • selector: 'app-root',
  • templateUrl: './app.component.html',
  • styleUrls: ['./app.component.css']
  • })
  • export class AppComponent {
  • value: any[] = [];
  • uploadUrl: string = "https://js.devexpress.com/Demos/NetCore/FileUploader/Upload";
  • onValueChanged(e) {
  • this.uploadUrl = this.updateQueryStringParameter("fileGuid", this.uuidv4());
  • }
  • updateQueryStringParameter(key, value) {
  • var re = new RegExp("([?&])" + key + "=.*?(&|$)", "i");
  • var separator = this.uploadUrl.indexOf("?") !== -1 ? "&" : "?";
  • if (this.uploadUrl.match(re)) {
  • return this.uploadUrl.replace(re, "$1" + key + "=" + value + "$2");
  • } else {
  • return this.uploadUrl + separator + key + "=" + value;
  • }
  • }
  • //https://stackoverflow.com/questions/105034/how-to-create-guid-uuid
  • uuidv4() {
  • return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, function(c) {
  • var r = (Math.random() * 16) | 0,
  • v = c === "x" ? r : (r & 0x3) | 0x8;
  • return v.toString(16);
  • });
  • }
  • }
  •  
  • @NgModule({
  • imports: [BrowserModule, DxFileUploaderModule],
  • declarations: [AppComponent],
  • bootstrap: [AppComponent]
  • })
  • export class AppModule {}