Skip to content

Instantly share code, notes, and snippets.

@tarot
Created June 29, 2015 10:33
Show Gist options
  • Select an option

  • Save tarot/a8e393ae488a1b5ed37e to your computer and use it in GitHub Desktop.

Select an option

Save tarot/a8e393ae488a1b5ed37e to your computer and use it in GitHub Desktop.
$ ->
$('input[type="file"]').on 'change', (event) ->
reader = new FileReader()
reader.onload = ->
boundary = "ljfdshflashdlfkjaklsdfjlksadjflksajdflksajdklfas"
blob = new Blob [
[
"--#{boundary}"
'Content-Disposition: form-data; name="json"'
'Content-Type: application/json; charset=UTF-8'
'\n'
'{"foo":"bar","hoge":"fuga"}'
'\n'
"--#{boundary}"
'Content-Disposition: form-data; name="file"'
'Content-Type: application/json; charset=UTF-8'
'\n'
].join('\n')
reader.result
[
'\n'
"--#{boundary}--"
].join('\n')
], type: "multipart/form-data; boundary=#{boundary}"
xhr = new XMLHttpRequest()
xhr.open 'POST', 'http://requestb.in/1dqyjp61'
xhr.onreadystatechange = ->
return unless xhr.readyState == XMLHttpRequest.DONE
if xhr.status >= 200 && xhr.status < 300
console.log xhr.responseText
else
alert xhr.statusText
xhr.send blob
reader.readAsArrayBuffer(event.target.files[0])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment