Created
June 29, 2015 10:33
-
-
Save tarot/a8e393ae488a1b5ed37e to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| $ -> | |
| $('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