12. How to Upload and Display Image in Angular (Drag and Drop Images)

Поделиться
HTML-код
  • Опубликовано: 18 дек 2024

Комментарии • 22

  • @ninosalkaton4461
    @ninosalkaton4461 2 года назад +2

    Thanks for the best tutorial, I have a question:
    How do get (display) these images in the user component?
    I think we should write another method in spring to get images.

  • @DavidMateusReis
    @DavidMateusReis 2 года назад +6

    I'm facing this error: Type 'null' is not assignable to type 'FileHandle'.
    @HostListener("drop", ["$event"])
    public onDrop(evt: DragEvent) {
    evt.preventDefault();
    evt.stopPropagation();
    this.background = "#eee";
    let fileHandle: FileHandle = null;
    const file = evt.dataTransfer.files[0];
    const url = this.sanitizer.bypassSecurityTrustUrl(window.URL.createObjectURL(file));
    fileHandle = {file, url};
    this.files.emit(fileHandle);
    }
    Solved:
    let fileHandle: FileHandle;
    const file: any = evt.dataTransfer?.files[0];
    const url = this.sanitizer.bypassSecurityTrustUrl(window.URL.createObjectURL(file));
    fileHandle = { file, url };
    this.files.emit(fileHandle);
    }

  • @keimasterV
    @keimasterV 2 года назад +1

    Hello, i implemented the directive as you, but when I try to use it, the background doesn't change, do you have any idea what could happen?

    • @LearnProgrammingYourself
      @LearnProgrammingYourself  2 года назад +1

      I think you can just check directive file line by line. Any small mistake will affect the functionality

    • @SamiKhan_15
      @SamiKhan_15 10 месяцев назад

      Hi. I was facing the same issue as you. Make sure to check your code line by line. Afterwards, stop the execution by "ctrl+c" and serve it again.

  • @Vikashkumar-gh1td
    @Vikashkumar-gh1td 2 года назад +1

    how can we add custom icon while dragging that image.

  • @dineshkumar-nk2me
    @dineshkumar-nk2me 2 года назад +1

    I am facing issue in drag. directive.spec.ts file
    "An argument for 'sanitizer' was not provided "

  • @sainadhparuchuri1928
    @sainadhparuchuri1928 2 года назад +1

    Bro next video please uplode user cart

  • @yulinliu9223
    @yulinliu9223 Год назад +1

    no git

  • @AlessioDiGiacomo-s4r
    @AlessioDiGiacomo-s4r Год назад +1

    Why this error?
    --> Argument of type 'Event' is not assignable to parameter of type 'FileHandle'.
    Type 'Event' is missing the following properties from type 'FileHandle': file, url
    ---HTML---

    Trascina e rilascia, oppure clicca su
    "scegli file"
    ---TS---
    fileDropped(fileHandle:FileHandle){
    this.product.imageUrl.push(fileHandle)
    }
    ---DIRECTIVE---
    @Output()
    files: EventEmitter = new EventEmitter();
    @HostBinding("style.background") private background = "#eee"
    constructor(private sanitizer: DomSanitizer){}
    @HostListener("dragover",["$event"])
    public onDragOver(evt: DragEvent){
    evt.preventDefault();
    evt.stopPropagation();
    this.background = "#999";
    }
    @HostListener("dragleave",["$event"])
    public onDragLeave(evt: DragEvent){
    evt.preventDefault();
    evt.stopPropagation();
    this.background = "#eee";
    }
    @HostListener("drop",["$event"])
    public onDrop(evt: DragEvent){
    evt.preventDefault();
    evt.stopPropagation();
    this.background = "#eee";
    let fileHandle:FileHandle;
    const file = evt.dataTransfer!.files[0];
    const url = this.sanitizer.bypassSecurityTrustUrl(window.URL.createObjectURL(file));
    fileHandle = {file, url};
    this.files.emit(fileHandle)

  • @botirovoybek
    @botirovoybek 2 года назад +1

    Drag and drop your file or
    browse a file


    ts
    ----------------
    fileDropped(fileHandle: Filehandle) {
    this.product.productImages.push(fileHandle);
    }
    !!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    why two types of arguments?
    not working

    • @fabianodias806
      @fabianodias806 2 года назад +1

      not working to me too.

    • @LearnProgrammingYourself
      @LearnProgrammingYourself  2 года назад

      Just focus on code. there are lot of small things used in the code.
      To answer your question.
      1st fileDropped($event) ----> this function is used in directive file.
      2nd fileDropped(fileHandle: FileHandle) ----> this function is used in ts file of the component.
      we can define function with the same name in different files. So that is the case here. Don't get confused with the names. The name seems same, but they are completely different from each other.