Created
July 25, 2013 15:40
-
-
Save eteubert/6081016 to your computer and use it in GitHub Desktop.
Create an Auphonic production. Then upload a file to that production.
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
| // Create the XHR object | |
| function createCORSRequest(method, url) { | |
| var xhr = new XMLHttpRequest(); | |
| if ("withCredentials" in xhr) { | |
| xhr.open(method, url, true); // XHR for Chrome/Firefox/Opera/Safari | |
| } else if (typeof XDomainRequest != "undefined") { | |
| xhr = new XDomainRequest(); // XDomainRequest for IE | |
| xhr.open(method, url); | |
| } else { | |
| xhr = null; // CORS not supported | |
| } | |
| return xhr; | |
| } | |
| function get_token() { | |
| return 'XXXXXXX'; // your token | |
| } | |
| var xhr = new createCORSRequest("POST", "https://auphonic.com/api/productions.json"); | |
| xhr.setRequestHeader("Content-type","application/json"); | |
| xhr.setRequestHeader("Authorization","Bearer " + get_token()); | |
| xhr.onload = function(e) { | |
| var response = JSON.parse(e.target.response), | |
| data = response.data, | |
| production_uuid = data.uuid, | |
| file = document.querySelector('#auphonic_local_upload_url').files[0]; | |
| console.log("Production: created"); | |
| if (file) { | |
| console.log("File Upload: started"); | |
| var url = 'https://auphonic.com/api/production/{uuid}/upload.json'.replace('{uuid}', production_uuid), | |
| xhr2 = new createCORSRequest("POST", url), | |
| formData = new FormData(); | |
| xhr2.setRequestHeader("Authorization","Bearer " + get_token()); | |
| xhr2.upload.addEventListener("progress", function(e) { console.log((e.loaded / e.total) * 100) }, false); | |
| xhr2.onload = function(e) { | |
| console.log("File Upload: Done"); | |
| }; | |
| formData.append('input_file', file); | |
| xhr2.send(formData); | |
| } | |
| }; | |
| xhr.send(JSON.stringify({"metadata":{"title":"test"}})); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment