Hi @Peli,
Sorry, I think the error we noticed is that our API should give a clearer error message when the wrong headers are sent with a file upload. Here’s a file uploader page I mocked up, I believe this should be closer:
<!DOCTYPE html>
<html>
<head>
<title></title>
<style>
form {
width: 95%;
height: 100px;
border: 1px solid #ccc;
}
progress {
width: 95%;
}
</style>
</head>
<body>
<form enctype="multipart/form-data">
<div>
<input name="file" type="file"/>
</div>
<div>
<input id="btnUpload" type="button" value="Upload"/>
</div>
</form>
<progress></progress>
<script src="https://code.jquery.com/jquery-1.10.1.min.js"></script>
<script>
var Page = {
startup: function () {
$('#btnUpload').attr('disabled', false);
$('#btnUpload').click(Page.onButtonClick);
$('progress').attr({value: 0, max: 10});
},
onButtonClick: function () {
$('#btnUpload').attr('disabled', true);
$('progress').attr({value: 1, max: 10});
Page.uploadFile();
},
uploadFile: function () {
var formData = new FormData($('form')[0]);
var a = $.ajax({
url: "https://api.spark.io/v1/devices/<DEVICE ID>",
type: "POST",
data: formData,
xhr: function () { // Custom XMLHttpRequest
var myXhr = $.ajaxSettings.xhr();
if (myXhr.upload) { // Check if upload property exists
myXhr.upload.addEventListener('progress', Page.onProgress, false); // For handling the progress of the upload
}
return myXhr;
},
cache: false,
contentType: false,
processData: false
});
a.done(Page.onUploadDone);
a.fail(Page.onUploadFailed);
},
onUploadDone: function () {
console.log('upload done!');
$('#btnUpload').attr('disabled', false);
},
onUploadFailed: function () {
console.error('upload failed ! ', arguments);
$('#btnUpload').attr('disabled', false);
},
onProgress: function (e) {
//http://stackoverflow.com/questions/166221/how-can-i-upload-files-asynchronously-with-jquery
if (e.lengthComputable) {
$('progress').attr({value: e.loaded, max: e.total});
}
}
};
Page.startup();
</script>
</body>
</html>