Skip to content

Instantly share code, notes, and snippets.

@trimsj
Created September 20, 2019 06:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save trimsj/1d6202e29e7550c7473b60ddbf67296e to your computer and use it in GitHub Desktop.
Save trimsj/1d6202e29e7550c7473b60ddbf67296e to your computer and use it in GitHub Desktop.
Minimum WebDAV client
<!DOCTYPE html>
<html>
<head>
<title>WebDAV</title>
<style>html, body, iframe {width: 100%; height: 100%}</style>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<h1>Choose files to upload</h1>
<input type="file" id="uploads" multiple><br><button>Submit</button>
<h1>Choose files to delete</h1>
<iframe src='.'></iframe>
<script>
count = 0;
button = document.querySelector('button');
iframe = document.querySelector('iframe');
function reload() {iframe.contentWindow.location.reload();}
function del(event) {
var target = event.target;
if (target.tagName == 'A' && confirm('Delete '+target.href)) {
var xhr = new XMLHttpRequest();
xhr.open('DELETE', target.href);
xhr.onload = reload;
xhr.send(null);
}
event.stopPropagation();
event.preventDefault();
}
iframe.onload = function(){iframe.contentWindow.onclick = del};
button.onclick = function(event) {
var file = document.querySelector('input').files;
count = file.length;
for(var i = 0; i < file.length; i++) {
button.disabled = true;
var xhr = new XMLHttpRequest();
xhr.open("PUT", new URL(file[i].name,window.location));
xhr.onreadystatechange = function() {
if (this.readyState === XMLHttpRequest.DONE) {
alert('HTTP '+this.status+'\n'+this.responseURL);
if (--count == 0) {button.disabled = false; reload();}
}
}
xhr.send(file[i]);
}
event.stopPropagation();
event.preventDefault();
};
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment