Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

File Upload in Angular 4

when I'm trying to install "npm install ng2-file-upload --save" in my angular 4 application it throws

UNMET PEER DEPENDENCY @4.1.0
UNMET PEER DEPENDENCY @4.1.0
`-- [email protected]

and upload is not working my applications throws

"Can't bind to 'uploader' since it isn't a known property of 'input'"

this is my HTML

<input type="file" ng2FileSelect [uploader]="upload" multiple formControlName="file" id="file"/>

and its Component

import { FileUploadModule } from 'ng2-file-upload/ng2-file-upload';   
import { FileSelectDirective, FileUploader } from 'ng2-file-upload/ng2-file-
upload';

export class PersonalInfoComponent implements OnInit
{
    public upload:FileUploader= new FileUploader({url:""});
}

Parent Module

import { FileUploadModule } from 'ng2-file-upload/ng2-file-upload';

@NgModule({

imports: [
..
....
..
FileUploadModule
],

export class RegistrationModule { }

and I didn't Import/change anything in AppModule(Grand Parent Module).

can someone help me on this please...

like image 926
sandeep Avatar asked May 16 '17 05:05

sandeep


People also ask

How do you create an HTML file to upload?

The <input type="file"> defines a file-select field and a "Browse" button for file uploads. To define a file-select field that allows multiple files to be selected, add the multiple attribute. Tip: Always add the <label> tag for best accessibility practices!


Video Answer


2 Answers

Upload images in Angular 4 without a plugin This is the article that might be worth trying. Upload images in Angular 4 without a plugin

It emphasize on these points:

  1. Using the .request() method instead of .post
  2. Sending formData straight into the body.
  3. Customizing header items and constructing a new RequestOptions object.
  4. In order to send formData with image content you must remove the Content-Type header.
like image 67
Assil Avatar answered Sep 22 '22 06:09

Assil


I don't think we need some extra libraries

onFileChange(event){
   let files = event.target.files; 
   this.saveFiles(files);
    }
@HostListener('dragover', ['$event']) onDragOver(event) {
    this.dragAreaClass = "droparea";
    event.preventDefault();
}
@HostListener('dragenter', ['$event']) onDragEnter(event) {
    this.dragAreaClass = "droparea";
    event.preventDefault();
}
@HostListener('dragend', ['$event']) onDragEnd(event) {
    this.dragAreaClass = "dragarea";
    event.preventDefault();
}
@HostListener('dragleave', ['$event']) onDragLeave(event) {
    this.dragAreaClass = "dragarea";
    event.preventDefault();
}
@HostListener('drop', ['$event']) onDrop(event) {   
    this.dragAreaClass = "dragarea";           
    event.preventDefault();
    event.stopPropagation();
    var files = event.dataTransfer.files;
    this.saveFiles(files);
}

And now we are ready to upload files with drag and drop as well as by clicking the link button and upload extra data with files.

See the complete article here Angular 4 upload files with data and web api by drag & drop

like image 32
Ali Adravi Avatar answered Sep 20 '22 06:09

Ali Adravi