Created
November 18, 2014 09:53
-
-
Save mankind/bbd896dd77590c0f1ddd to your computer and use it in GitHub Desktop.
Emberjs file upload with HTML-5 formData
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Ember from 'ember'; | |
var AssetsRoute = Ember.Route.extend({ | |
model: function() { | |
return this.store.find('attachment'); | |
}, | |
actions: { | |
saveUpload: function(formData){ | |
console.log('save', formData); | |
var self = this; | |
var uploadToServer = Ember.$.ajax({ | |
url: '/api/attachments.json', | |
data: formData, | |
cache: false, | |
contentType: false, | |
processData: false, | |
dataType: 'json', | |
type: 'POST' | |
}); | |
uploadToServer.then( | |
function(data){ | |
console.log('uploaded data', data); | |
//load it into ember-data store | |
var a = self.get('store').pushPayload('attachment', data); | |
console.log('asset model after upload', a); | |
//self.transitionTo('assets.asset', data); | |
}, | |
function(error){ | |
console.log('upload error', error); | |
} | |
); //closes then | |
} //coses saveUpload | |
} //closes actions | |
}); | |
export default AssetsRoute; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Ember from 'ember'; | |
var UploadFileComponent = Ember.Component.extend({ | |
actions: { | |
sendUpload: function(model){ | |
//works | |
//var formdata = new FormData($('form')); | |
//var formdata = new FormData(document.getElementById('upload')) | |
//fd = new FormData($('#upload')); | |
//http://stackoverflow.com/questions/16762524/html5-file-upload-what-should-be-append-to-formdata | |
//also works | |
//Note that the string we appended to formData, "assets[attachment]", corresponds to how your controller expects your params. | |
var formdata = new FormData(); | |
//formdata.append("asset[attachment]", model.attachment); | |
formdata.append("attachment[image]", model.image); | |
console.log('form model', model); | |
//this.sendAction('action', model); | |
this.sendAction('action', formdata); | |
} | |
} //closes actions | |
}); | |
export default UploadFileComponent; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment