Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
1.2 kB
2
Indexable
Never
import { Component } from '@angular/core';

@Component({
  // component metadata
})
export class YourComponent {

  onDrop(event: DragEvent, type: string) {
    event.preventDefault();
    if (event.dataTransfer && event.dataTransfer.files) {
      const file = event.dataTransfer.files[0]; // Assuming it's an image
      if (file && file.type.match('image.*')) {
        this.readFileAsDataUrl(file);
      }
    }
  }

  readFileAsDataUrl(file: File) {
    const reader = new FileReader();
    reader.onload = (e: any) => {
      const img = new Image();
      img.onload = () => {
        const canvas = document.createElement('canvas');
        const ctx = canvas.getContext('2d');
        canvas.width = img.width;
        canvas.height = img.height;
        ctx.drawImage(img, 0, 0);
        const imageDataUrl = canvas.toDataURL(); // or convert to bitmap as needed
        this.sendImageToApi(imageDataUrl);
      };
      img.src = e.target.result;
    };
    reader.readAsDataURL(file);
  }

  sendImageToApi(imageData: string) {
    // Use Angular HttpClient to send data to your API
  }

  allowDrop(event: DragEvent) {
    event.preventDefault();
  }
}
Leave a Comment