angular-file icon indicating copy to clipboard operation
angular-file copied to clipboard

Request: emit newly added files separately

Open rhuang opened this issue 5 years ago • 6 comments

Looking at: https://github.com/AckerApple/angular-file/blob/development/src/file-upload/ngf.directive.ts#L169

It seems like files contains the newly added files. Can we emit files as an event here so that we can take action on the newly added files? I couldn't find any such event in the documentation or in my brief scan of the source code.

rhuang avatar Jun 17 '19 23:06 rhuang

(filesChange)

AckerApple avatar Jun 18 '19 01:06 AckerApple

https://github.com/AckerApple/angular-file/blob/development/src/file-upload/ngf.directive.ts#L174

AckerApple avatar Jun 18 '19 12:06 AckerApple

Doesn't filesChange emit all the existing files, the new and the old, since files is already pushed into this.files at that point?

I was thinking something like this:

que( files:File[] ){
  this.files = this.files || []

  // ADDED HERE, since `files` contains the files that were just selected
  this.newFiles.emit(files);

  Array.prototype.push.apply(this.files, files) // now, `this.files` contains the the old and the new files
    
  this.filesChange.emit( this.files ) // when I receive `filesChange` at this point, I can't tell which ones were just added and which ones were old
  ...
}

rhuang avatar Jun 18 '19 13:06 rhuang

Perhaps. You will need to issue a pull request for this if you wanted it.

I'm sure you could easily enough control knowing new files from existing on your own code. You could save the array of files to a different scope and just clear the files array every time files are selected

AckerApple avatar Jun 18 '19 14:06 AckerApple

Yeah, that's what I'm doing at the moment, though it seems to make more sense to emit the diff from this directive. I'll see if I'll get a chance to issue a pull request later this week.

rhuang avatar Jun 18 '19 17:06 rhuang

@rhuang if this is still an issue for you, I recently came up with a rather simple workaround for this. If you don't specify [(files)]="..." in your ngf directive, but do specify (filesChange)="someMethod($event)" the $event sends an array with just one file (the one you just added), then you can use someMethod() to perform any operations on that file you need, then you can push that file to your "files" array manually if you need to.

ShiftyMcCool avatar Mar 23 '20 18:03 ShiftyMcCool