FileReader upload File through Ember Data


#1

I have an old Rails form that I want to translate into Ember Data:

<form>
  <input type="file" name="myfile" />
  <input type="submit" />
</form>

What gets stored in the database needs to match what I currently do.  Users can upload files of all types, not just binary.  I choose a particular image, and in the database, the file data begins with this:

[1] pry(#<AttachmentsController>)> @attachment.data => "\xC3\xBF\xC3\x98\xC3\xBF\xC3\xA2\x02\x1CICC_PROFILE\x00

I want to convert the form to upload using javascript and Ember.JS Data, not form submission.  The javsacript which uploads my data is this:

var attachment = this.store.createRecord('attachment');
attachment.set('data', data);
attachment.save();

The problem is data.  When it gets to the server, it’s different than how it was for the form.  I’ve tried reading the data in multiple ways:

readAsBinaryString produced a different string:

reader.readAsBinaryString(input.files[0]);
// Note: readAsBinaryString is DEPRECATED
[1] pry(#<AttachmentsController>)> @attachment.data => "\xC3\xBF\xC3\x98\xC3\xBF\xC3\xA2\x02\x1CICC_PROFILE\x00

Read as text would occassionally crash when uploading a binary file

reader.readAsText(input.files[0]);
// crashes

readAsDataURL produced results that didn’t match what I need to store in my database

reader.readAsDataURL(input.files[0]);
[1] pry(#<AttachmentsController>)> @attachment.data => "data:image/jpeg;base64,/9j/4gIcSUNDX1BST0ZJTEUAAQEAAAIMbGNtcwIQAABtb

readAsArrayBuffer looks promising, but I’m not sure what I should do with the arrayBuffer to send

Any help is appreciated!

Note: This is a crosspost of what’s on StackOverflow. I received no responses there after 8 days. http://stackoverflow.com/questions/23898104/filereader-upload-file-through-ember-data