Project

General

Profile

1
<html>
2
<body>
3
<script>
4
var url = "post.php";
5
var binary;
6
var filename;
7
var mytext;
8

    
9
function upload() {
10
	filename = document.getElementById('myfile').value;
11
	mytext = document.getElementById('mytext').value;
12
	document.getElementById('ajaxbutton').disabled = true;
13

    
14
	
15
	
16
	// open the local file
17
	var file = Components.classes["@mozilla.org/file/local;1"]
18
		.createInstance(Components.interfaces.nsILocalFile);
19
	file.initWithPath( filename );		
20
	stream = Components.classes["@mozilla.org/network/file-input-stream;1"]
21
		.createInstance(Components.interfaces.nsIFileInputStream);
22
	stream.init(file,	0x01, 00004, null);
23
	var bstream =  Components.classes["@mozilla.org/network/buffered-input-stream;1"]
24
		.getService();
25
	bstream.QueryInterface(Components.interfaces.nsIBufferedInputStream);
26
	bstream.init(stream, 1000);
27
	bstream.QueryInterface(Components.interfaces.nsIInputStream);
28
	binary = Components.classes["@mozilla.org/binaryinputstream;1"]
29
		.createInstance(Components.interfaces.nsIBinaryInputStream);
30
	binary.setInputStream (stream);
31

    
32
	// start AJAX file upload in 1 second
33
	window.setTimeout("ajax_upload()", 1000);
34
}
35

    
36
function ajax_upload() {
37
	// request more permissions
38
	
39

    
40
	http_request = false;
41
	http_request = new XMLHttpRequest();
42
	if (!http_request) {
43
		alert('Cannot create XMLHTTP instance');
44
		return false;
45
	}
46

    
47
	// prepare the MIME POST data
48
	var boundaryString = 'capitano';
49
	var boundary = '--' + boundaryString;
50
	var requestbody = boundary + '\n' 
51
	+ 'Content-Disposition: form-data; name="mytext"' + '\n' 
52
	+ '\n' 
53
	+ mytext + '\n' 
54
	+ '\n' 
55
	+ boundary + '\n' 
56
	+ 'Content-Disposition: form-data; name="myfile"; filename="' 
57
		+ filename + '"' + '\n' 
58
	+ 'Content-Type: application/octet-stream' + '\n' 
59
	+ '\n'
60
	+ escape(binary.readBytes(binary.available()))
61
	+ '\n'
62
	+ boundary;
63

    
64
	document.getElementById('sizespan').innerHTML = 
65
		"requestbody.length=" + requestbody.length;
66
	
67
	// do the AJAX request
68
	http_request.onreadystatechange = requestdone;
69
	http_request.open('POST', url, true);
70
	http_request.setRequestHeader("Content-type", "multipart/form-data; \
71
		boundary=\"" + boundaryString + "\"");
72
	http_request.setRequestHeader("Connection", "close");
73
	http_request.setRequestHeader("Content-length", requestbody.length);
74
	http_request.send(requestbody);
75

    
76
}
77

    
78
function requestdone() {
79
	if (http_request.readyState == 4) {
80
		if (http_request.status == 200) {
81
			result = http_request.responseText;
82
			document.getElementById('myspan').innerHTML = result;            
83
		} else {
84
			alert('There was a problem with the request.');
85
		}
86
		document.getElementById('ajaxbutton').disabled = false;
87
	}
88
}
89

    
90
</script>
91

    
92
<form>
93
Text: <input type="text" id="mytext" name="mytext" size="40">
94
<br>
95
File: <input type="file" id="myfile" name="datafile" size="40"><br>
96
<input type="button" id="ajaxbutton" value="AJAX IT" onclick="upload();">
97
</form>
98

    
99
<div id="sizespan"></div>
100
<hr>
101
<div id="myspan"></div>
102

    
103
</body>
104
</html>
(13-13/15)