Hello, I want to use ajax upload I m using "commons-fileupload.jar" for uploading file... and below code is used to call servlet through ajax. Actually I am new in ajax so I can't understand what happens while I use multipart request.
My javascript is
<script language="javascript">
function getXMLObject() //XML OBJECT
{
var xmlHttp = false;
try {
xmlHttp = new ActiveXObject("Msxml2.XMLHTTP") // For Old Microsoft Browsers
}
catch (e) {
try {
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP") // For Microsoft IE 6.0+
}
catch (e2) {
xmlHttp = false // No Browser accepts the XMLHTTP Object then false
}
}
if (!xmlHttp && typeof XMLHttpRequest != 'undefined') {
xmlHttp = new XMLHttpRequest(); //For Mozilla, Opera Browsers
}
return xmlHttp; // Mandatory Statement returning the ajax object created
}
var xmlhttp = new getXMLObject(); //xmlhttp holds the ajax object
function addPhoto(){
alert("before if");
var f = document.form1.file;
alert(f);
alert(f1.value);
if(xmlhttp) {
alert("inside if");
xmlhttp.open("GET","ajaxUploadGenerator?file="+f,true); //ajaxUploadGeneratoris servlet name
xmlhttp.onreadystatechange = handleServerResponse;
xmlhttp.setRequestHeader('Content-Type','multipart/form-data');
xmlhttp.send(null);
}
alert("after if");
}
function handleServerResponse() {
if (xmlhttp.readyState == 4) {
if(xmlhttp.status == 200) {
document.getElementById("reply").innerHTML = xmlhttp.responseText;
}
else {
alert("Error during AJAX call. Please try again");
}
}
}
</script>
and My page contains...
<form id="form1" name="form1" enctype="multipart/form-data">
<label>
<input type="file" name="file" />
</label>
<p>
<label>
<input type="button" name="Submit" value="Submit" onclick="addPhoto()"/>
<div id="reply"></div>
</label>
</p>
<iframe name="iframe" id="iframe" style="display:none">
</iframe>
</form>