Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-03-2012, 01:40 PM   PM User | #1
Gatsu
New to the CF scene

 
Join Date: Jan 2011
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Gatsu is an unknown quantity at this point
file uploader progress bars

Hi, I am trying to make a script that will upload files and show a progress bar and I've come so far that I can upload the file but the progress bar that appear for each file will not grow with green goo, you know?

Please help!

This is a drop box version:
Code:
window.onload = function()
{
	var dropzone = document.getElementById("dropArea");
	dropzone.ondragover = dropzone.ondragenter = function(event)
	{
		event.stopPropagation();
		event.preventDefault();
	}

	dropzone.ondrop = function(event)
	{
		event.stopPropagation();
		event.preventDefault();

		var filesArray = event.dataTransfer.files;
		for (int i=0; i<filesArray.length; i++)
		{
			var progressDiv = document.getElementById('progressDiv');
			var pbar = document.createElement('progress');
			var br = document.createElement('br');
			var report = document.createElement('div');
			
			pbar.setAttribute('id', 'progressBar' + i);
			pbar.setAttribute('value', '0');
			pbar.setAttribute('max', '100');
			report.setAttribute('id', 'report' + i);
			
			progressDiv.appendChild(pbar);
			progressDiv.appendChild(br);
			progressDiv.appendChild(report);
			progressDiv.appendChild(br);
			
			sendFile(filesArray[i]);
		}
	}
}


function sendFile(file)
{
	var uri = "upload.php";
	var xhr = new XMLHttpRequest();
	var fd = new FormData();

	xhr.upload.addEventListener("progress", progressFunction, false);
	
	xhr.open("POST", uri, true);
	xhr.onreadystatechange = function()
	{
		if (xhr.readyState == 4 && xhr.status == 200)
		{
			// Handle response.
			//alert(xhr.responseText);
			var percentageDiv = document.getElementById("report");
			percentageDiv.innerHTML = 'Transfer complete';
		}
	}
	fd.append('myFile', file);
	// Initiate a multipart/form-data upload
	xhr.send(fd);
}


function progressFunction(evt)
{
	var progressBar = document.getElementById('progressBar');
	var percentageDiv = document.getElementById('report');
	while(evt.lengthComputable)
	{
		progressBar.max = evt.total;
		progressBar.value = evt.loaded;
		percentageDiv.innerHTML = Math.round(evt.loaded / evt.total * 100) + "%";
	}
}

Last edited by Gatsu; 12-04-2012 at 10:55 AM..
Gatsu is offline   Reply With Quote
Old 12-03-2012, 02:40 PM   PM User | #2
devnull69
Senior Coder

 
Join Date: Dec 2010
Posts: 2,245
Thanks: 10
Thanked 531 Times in 525 Posts
devnull69 will become famous soon enough
With this line
Code:
var progressBar = document.getElementById('progressBar');
you are trying to access a <progress> element with id="progressBar", but with this code
Code:
pbar.setAttribute('id', 'progressBar' + i);
you created the <progress> elements with id="progressBar0", "progressBar1" etc. respectively. So there is no <progress> element with id="progressBar". The same can be applied to the <div> with id="report"
devnull69 is offline   Reply With Quote
Old 12-03-2012, 06:58 PM   PM User | #3
rnd me
Senior Coder

 
rnd me's Avatar
 
Join Date: Jun 2007
Location: Urbana
Posts: 3,455
Thanks: 9
Thanked 466 Times in 450 Posts
rnd me is a jewel in the roughrnd me is a jewel in the roughrnd me is a jewel in the rough
Quote:
Originally Posted by Gatsu View Post
will not grow with green goo, you know?
lol! best. question. ever. lovin it.
__________________
my site (updated 5/13)
STATS (2013/5) HTML5:90.2% MOB:14% IE7:0.5% IE8:8.6% IE9:9.8% IE10:10%
rnd me is offline   Reply With Quote
Old 12-04-2012, 11:06 AM   PM User | #4
Gatsu
New to the CF scene

 
Join Date: Jan 2011
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Gatsu is an unknown quantity at this point
so I have to pass i to function progressFunction(evt)

but now the script just decided not to work at all, when i drop files they open in my browser hmm... I changed nothing!

my html is simple like this:

Code:
<script type="text/javascript" src="dropbox.js"></script>
<div id="dropArea">Drop Area</div>
<div id="progressDiv"></div>
Might anybody know why this happened all of a sudden?
Gatsu is offline   Reply With Quote
Old 12-05-2012, 10:57 AM   PM User | #5
Gatsu
New to the CF scene

 
Join Date: Jan 2011
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Gatsu is an unknown quantity at this point
Can it be because I'm now using notepad++ to edit this script?
Gatsu is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 12:56 PM.


Advertisement
Log in to turn off these ads.