Skip to content

Instantly share code, notes, and snippets.

@domenic
Created March 20, 2012 17:13
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save domenic/2138293 to your computer and use it in GitHub Desktop.
Save domenic/2138293 to your computer and use it in GitHub Desktop.
File upload using XHR PUT
(function () {
var $dropTarget = $("#drop-target");
var slice = Function.prototype.call.bind(Array.prototype.slice);
$dropTarget.on({
drop: function (event) {
slice(event.originalEvent.dataTransfer.files).forEach(putFile);
return false;
},
dragover: false
});
function putFile(file) {
var xhr = new XMLHttpRequest();
xhr.upload.addEventListener("progress", function (e) {
if (e.lengthComputable) {
console.log(e.loaded / e.total);
}
});
xhr.upload.addEventListener("load", function () {
console.log("uploaded");
});
xhr.open("PUT", "http://localhost:7070/123/fromXHR.pdf");
xhr.overrideMimeType(file.type);
xhr.send(file);
}
}());
@JoxieMedina
Copy link

Why when i try this I get a 405 (Method Not Allowed) error ?

@soapysmiles
Copy link

This is a good snippet :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment