Add custom attributes to body #226

Open
opened 2016-05-27 15:21:24 +00:00 by JunusErgin · 4 comments
JunusErgin commented 2016-05-27 15:21:24 +00:00 (Migrated from github.com)

Hello,

i want to add custom attributes (like description and size) to the body of each item before uploading.
Would be nice if you provide a function to do so.

Best regards and thanks for your support!

Hello, i want to add custom attributes (like description and size) to the body of each item before uploading. Would be nice if you provide a function to do so. Best regards and thanks for your support!
cmyip commented 2016-06-14 06:08:31 +00:00 (Migrated from github.com)

You can use onBuildItemForm to set the form body

this.uploader.onBuildItemForm = function(fileItem, form){ form.append('myvar', 'myval'); return {fileItem, form} }; this.uploader.queue[0].upload();

You can use onBuildItemForm to set the form body `this.uploader.onBuildItemForm = function(fileItem, form){ form.append('myvar', 'myval'); return {fileItem, form} }; this.uploader.queue[0].upload(); `
JunusErgin commented 2016-06-28 15:25:35 +00:00 (Migrated from github.com)

Thank you very much!
Best greetings from Germany! :-)

Thank you very much! Best greetings from Germany! :-)
jiggy1com commented 2016-12-21 08:47:05 +00:00 (Migrated from github.com)

I can probably figure it out, but the 2nd baby is only a couple weeks old and my brain is fried... Is it possible to append a var to a single fileItem rather than all?

UPDATE:

i did something like this:

// this handles adding a property to the individual fileItem
this.uploader.onCompleteItem = (fileItem, response, status, headers) => {
  var actualResponse = JSON.parse(fileItem._xhr.response);
  if(status == 200){
    if(!actualResponse.success){
      fileItem.duplicate = actualResponse.message.indexOf('Duplicate') >= 0;
    }
  }
}

in my view:

<!-- show force overwrite button if a duplicate file was found -->
<div *ngIf="item.duplicate">
	A file with the same filename was found. Do you wish to overwrite?
	<button *ngIf="item.duplicate" (click)="forceOverwrite(idx)" class="btn btn-primary">
		Force Overwrite
	</button>
</div>

and back in my component

forceOverwrite(idx){
  this.uploader.onBuildItemForm = function(fileItem, form){
      // as the uploader goes through the fileItems, and
      // if the fileItem contains the added property, then
      // append the form
      if(fileItem.duplicate) {
        form.append('overwrite', 'true');
      }
    return {fileItem, form}
  };
  this.uploader.queue[idx].upload();
}

Thanks @cmyip for the initial code help

I can probably figure it out, but the 2nd baby is only a couple weeks old and my brain is fried... Is it possible to append a var to a single fileItem rather than all? UPDATE: i did something like this: ``` // this handles adding a property to the individual fileItem this.uploader.onCompleteItem = (fileItem, response, status, headers) => { var actualResponse = JSON.parse(fileItem._xhr.response); if(status == 200){ if(!actualResponse.success){ fileItem.duplicate = actualResponse.message.indexOf('Duplicate') >= 0; } } } ``` in my view: ``` <!-- show force overwrite button if a duplicate file was found --> <div *ngIf="item.duplicate"> A file with the same filename was found. Do you wish to overwrite? <button *ngIf="item.duplicate" (click)="forceOverwrite(idx)" class="btn btn-primary"> Force Overwrite </button> </div> ``` and back in my component ``` forceOverwrite(idx){ this.uploader.onBuildItemForm = function(fileItem, form){ // as the uploader goes through the fileItems, and // if the fileItem contains the added property, then // append the form if(fileItem.duplicate) { form.append('overwrite', 'true'); } return {fileItem, form} }; this.uploader.queue[idx].upload(); } ``` Thanks @cmyip for the initial code help
pikezeed commented 2017-01-09 06:38:19 +00:00 (Migrated from github.com)

Thanks @cmyip for answer it' help for me so much

Thanks @cmyip for answer it' help for me so much
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: dc/ng2-file-upload#226