<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Junk - Test File</title>
<link href="/css/popup.css" rel="stylesheet" type="text/css">

<!-- Need to get slices working for large files.  -->

<script type="text/javascript">

  //  Original idea for Base64 encode / decode found at: http://www.webtoolkit.info/
 
  var Base64 = {
 
	// lookup string for Base64 conversion
	enc_lookup : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",

	// Convert binary string into MIME::Base64, 57 bytes at a time.
	encode : function (input) {
		var output = "";
		var i = 0;
		while (i < input.length) {
			output = output + Base64.encode_57b(input.substr(i,57)) + "\n";
			i += 57;
		}
		return output;
	},
 
	// Base64 encode a 57 byte chunk of the file
	encode_57b : function (line_b57) {
		var output = "";
		var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
		var i = 0;
 
		while (i < line_b57.length) {
 
			chr1 = line_b57.charCodeAt(i++);
			chr2 = line_b57.charCodeAt(i++);
			chr3 = line_b57.charCodeAt(i++);
 
			enc1 = chr1 >> 2;
			enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
			enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
			enc4 = chr3 & 63;
 
			if (isNaN(chr2)) {
				enc3 = enc4 = 64;
			} else if (isNaN(chr3)) {
				enc4 = 64;
			}
 
			output = output +
			this.enc_lookup.charAt(enc1) + this.enc_lookup.charAt(enc2) +
			this.enc_lookup.charAt(enc3) + this.enc_lookup.charAt(enc4);
 
		}
		return output;
	}
  }

  // Put binary file to server
  function put_bin_file($Part,$FileName,$FileSize,$FileContents){ 
  	var xmlhttp; 
	var resource = "http://foobar.com.au/cgi-bin/put_post.pl";
	var contents = $FileContents;
	xmlhttp=new XMLHttpRequest(); 
	xmlhttp.open("POST",resource,false);
	xmlhttp.setRequestHeader("FILEPUT_USER","FOO")
	xmlhttp.setRequestHeader("FILEPUT_SSID","9999")
	xmlhttp.setRequestHeader("FILEPUT_NAME",$FileName)
	xmlhttp.setRequestHeader("FILEPUT_SIZE",$FileSize)
	xmlhttp.setRequestHeader("FILEPUT_PART",$Part)
	xmlhttp.send(Base64.encode(contents)); 
  } 

</script>

</head>

<body>

<div>

<hr>
<h2>Test HTML PUT method</h2>

<input type="file" id="fileinput" />
<script type="text/javascript">
  function readSingleFile(evt) {
	var File = evt.target.files[0]; 
	var part_size = 1048576;
	// alert( "DEBUG INFO: " +
		// "name: " + File.name + " | " +
		// "type: " + File.type + " | " +
		// "size: " + File.size + " bytes");
	if (File) {
		if ( File.size < part_size + 1) {
			var FileIn = new FileReader();
			FileIn.onload = function(evt) { 
				var contents = evt.target.result;
				put_bin_file(0,File.name,File.size,contents);
			}
			FileIn.readAsBinaryString(File);
		} else {
			// Chop it up into chunks and send it to the server.
			var FileIn = new FileReader();
			FileIn.onload = function(evt) { 
				var contents = evt.target.result;
				var n = 1
				var begin = 0;
				while (begin <= File.size) {
					put_bin_file(n++,File.name,File.size,contents.substr(begin,part_size));
					begin += part_size;
				}
			}
			FileIn.readAsBinaryString(File);
		}
	} else { 
		alert("Failed to load file");
	}
  }
  document.getElementById('fileinput').addEventListener('change', readSingleFile, false);
</script>

<div>

  <!-- This does not work ... All newbies go down this path first it seems -->

  <!-- form action="http://foobar.com.au/cgi-bin/put_file.pl" method="put" >

    <input type="file" name="imagefile" value="" />
    <input type="submit" />
  </form -->

</div>

</body>
</html>