Skip to content

Instantly share code, notes, and snippets.

@grapho
Last active June 6, 2016 16:19
Show Gist options
  • Select an option

  • Save grapho/b93ff79371d49865aff19db757a8c93f to your computer and use it in GitHub Desktop.

Select an option

Save grapho/b93ff79371d49865aff19db757a8c93f to your computer and use it in GitHub Desktop.
Upload Queue
import Ember from 'ember';
const {
RSVP,
$: { ajax }
} = Ember;
export default Ember.Controller.extend({
appName: 'Uploading Hell',
formData: Ember.computed(function() {
return new FormData();
}),
queuedFiles: Ember.computed(function() {
return this.get('formData').getAll('file[]');
}),
sendFileAttachments(id) {
console.log('sendFileAttachments');
let url = `api/rest/account/document?account_id=${id}`;
let files = this.get('queuedFiles');
let formData = this.get('formData');
console.log(formData.getAll('file[]'));
return ajax({
url: url,
cache: false,
method: 'POST',
data: formData,
processData: false,
contentType: false,
headers: {
'Accept': 'application/json; version=1.0.0',
}
});
},
actions: {
addFileToQueue(FileList) {
console.log('FileList');
console.log(FileList);
let formData = this.get('formData');
for (let i = 0; i < FileList.length; i++) {
formData.append(`file[]`, FileList[i], FileList[i].name);
}
this.notifyPropertyChange('queuedFiles');
},
upload() {
this.sendFileAttachments(1);
}
}
});
<h1>Welcome to {{appName}}</h1>
<br>
<br>
<input type="file" multiple onchange={{action "addFileToQueue" value="target.files"}}>
<hr>
Queued Files:
<ul>
{{#each queuedFiles as |file|}}
<li>{{file.name}}</li>
{{/each}}
</ul>
<hr>
<button onclick={{action "upload"}}>Upload</button>
{
"version": "0.8.1",
"EmberENV": {
"FEATURES": {}
},
"options": {
"use_pods": false,
"enable-testing": false
},
"dependencies": {
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.3/jquery.js",
"ember": "2.5.1",
"ember-data": "2.5.3",
"ember-template-compiler": "2.5.1"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment