ngx-uploader icon indicating copy to clipboard operation
ngx-uploader copied to clipboard

How to limit the mime-type of uploaded file? (eg. gif, jpeg, png)

Open lliiuu opened this issue 7 years ago • 6 comments

help me,thanks

lliiuu avatar Dec 29 '17 08:12 lliiuu

Look at the README.md section "Upload Restrictions". There is a link to an example.

retailify avatar Dec 29 '17 08:12 retailify

@retailify that link does not offer any help regarding this issue at all.

coolsam726 avatar Jan 16 '18 14:01 coolsam726

@coolsam726 Please make your question more specific.

retailify avatar Jan 16 '18 14:01 retailify

@retailify I am trying to figure out how to restrict file upload types. The Readme provides a link to app-home.component whose code is given below:

import { Component, EventEmitter } from '@angular/core';
import { UploadOutput, UploadInput, UploadFile, humanizeBytes, UploaderOptions, UploadStatus } from '../../../';

@Component({
  selector: 'app-home',
  templateUrl: 'app-home.component.html'
})
export class AppHomeComponent {
  formData: FormData;
  files: UploadFile[];
  uploadInput: EventEmitter<UploadInput>;
  humanizeBytes: Function;
  dragOver: boolean;
  options: UploaderOptions;

  constructor() {
    this.options = { concurrency: 1 };
    this.files = [];
    this.uploadInput = new EventEmitter<UploadInput>();
    this.humanizeBytes = humanizeBytes;
  }

  onUploadOutput(output: UploadOutput): void {
    if (output.type === 'allAddedToQueue') {
      const event: UploadInput = {
        type: 'uploadAll',
        url: 'http://ngx-uploader.com/upload',
        method: 'POST',
        data: { foo: 'bar' }
      };

      this.uploadInput.emit(event);
    } else if (output.type === 'addedToQueue'  && typeof output.file !== 'undefined') {
      this.files.push(output.file);
    } else if (output.type === 'uploading' && typeof output.file !== 'undefined') {
      const index = this.files.findIndex(file => typeof output.file !== 'undefined' && file.id === output.file.id);
      this.files[index] = output.file;
    } else if (output.type === 'removed') {
      this.files = this.files.filter((file: UploadFile) => file !== output.file);
    } else if (output.type === 'dragOver') {
      this.dragOver = true;
    } else if (output.type === 'dragOut') {
      this.dragOver = false;
    } else if (output.type === 'drop') {
      this.dragOver = false;
    } else if (output.type === 'rejected' && typeof output.file !== 'undefined') {
      console.log(output.file.name + ' rejected');
    }

    this.files = this.files.filter(file => file.progress.status !== UploadStatus.Done);
  }

  startUpload(): void {
    const event: UploadInput = {
      type: 'uploadAll',
        url: 'http://ngx-uploader.com/upload',
        method: 'POST',
      data: { foo: 'bar' }
    };

    this.uploadInput.emit(event);
  }

  cancelUpload(id: string): void {
    this.uploadInput.emit({ type: 'cancel', id: id });
  }

  removeFile(id: string): void {
    this.uploadInput.emit({ type: 'remove', id: id });
  }

  removeAllFiles(): void {
    this.uploadInput.emit({ type: 'removeAll' });
  }
}

I have scanned the code but I still can't see anywhere there is an example of how to limit the upload types. Am I missing something?

coolsam726 avatar Jan 16 '18 14:01 coolsam726

you are right. 2 days ago it was removed from the file.

replace the constructor

constructor() {
    this.options = { concurrency: 1 };
    this.files = [];
    this.uploadInput = new EventEmitter<UploadInput>();
    this.humanizeBytes = humanizeBytes;
  }

with

constructor() {
    this.options = { concurrency: 0, allowedContentTypes: ['image/jpeg', 'image/png', 'image/gif'] };
    this.files = [];
    this.uploadInput = new EventEmitter<UploadInput>();
    this.humanizeBytes = humanizeBytes;
  }

I'll hope, this helps.

retailify avatar Jan 16 '18 14:01 retailify

That definitely helps. Thank you very much @retailify

coolsam726 avatar Jan 16 '18 14:01 coolsam726