Hi
I have a page which lets me upload files using HTML5/JavaScript. Files get uploaded after being dragged and dropped. This part is working very well.
What I would like to do is add a button which gives the user an alternative method - i.e. they can click a button, browse to the directory and then select the file(s) they want to upload.
So far I have:
What I would like it to do is behave in the same way as dragging and dropping - currently it redirects the whole browser to my upload.asp script but what I want it do is post the file(s) via AJAX and show the progress bars.
Hope someone can help!
Thanks very much
Ed
I have a page which lets me upload files using HTML5/JavaScript. Files get uploaded after being dragged and dropped. This part is working very well.
Code:
<!DOCTYPE html>
<html>
<head>
<title>dnd binary upload</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script type="text/javascript">
function sendFile(file) {
var uri = "/upload/upload.asp";
var xhr = new XMLHttpRequest();
var fd = new FormData();
// create progress bar
var o = document.getElementById('progress');
var progress = o.appendChild(document.createElement("p"));
progress.appendChild(document.createTextNode("upload " + file.name));
// progress bar
xhr.upload.addEventListener("progress", function(e) {
var pc = parseInt(100 - (e.loaded / e.total * 100));
progress.style.backgroundPosition = pc + "% 0";
}, false);
xhr.open("POST", uri, true);
xhr.onreadystatechange = function() {
if (xhr.readyState == 4) {
// Handle response.
//alert(xhr.responseText); // handle response.
progress.innerHTML=progress.innerHTML + ' - DONE';
}
};
fd.append('myFile', file);
fd.append("FUID", "12345-ABC");
// Initiate a multipart/form-data upload
xhr.send(fd);
}
window.onload = function() {
var dropzone = document.getElementById("dropzone");
dropzone.ondragover = dropzone.ondragenter = function(event) {
event.stopPropagation();
event.preventDefault();
}
dropzone.ondrop = function(event) {
event.stopPropagation();
event.preventDefault();
var filesArray = event.dataTransfer.files;
for (var i=0; i<filesArray.length; i++) {
sendFile(filesArray[i]);
}
}
}
</script>
</head>
<style>
#progress p
{
display: block;
width: 240px;
padding: 2px 5px;
margin: 2px 0;
border: 1px inset #446;
border-radius: 5px;
background: #eee url("progress.png") 100% 0 repeat-y;
}
#progress p.success
{
background: #0c0 none 0 0 no-repeat;
}
#progress p.failed
{
background: #c00 none 0 0 no-repeat;
}
</style>
<body>
<div id="progress"></div>
<div>
<div id="dropzone" style="margin:30px; width:500px; height:300px; border:1px dotted grey;">Drag & drop your file here...</div>
</div>
</body>
</html>
What I would like to do is add a button which gives the user an alternative method - i.e. they can click a button, browse to the directory and then select the file(s) they want to upload.
So far I have:
Code:
<form action="upload.asp" enctype="multipart/form-data" method="post">
<input id="fileupload" type="file" name="myFile" data-url="upload.asp" multiple>
<input type="submit">
</form>
What I would like it to do is behave in the same way as dragging and dropping - currently it redirects the whole browser to my upload.asp script but what I want it do is post the file(s) via AJAX and show the progress bars.
Hope someone can help!
Thanks very much
Ed