function createIFrame() {
var id = 'f' + Math.floor(Math.random() * 99999);
var div = document.createElement('div');
div.innerHTML = '<IFRAME style="display:none" src="about:blank" id="'+id+'" name="'+id+'" onload="sendComplete(\''+id+'\')"></IFRAME>';
document.body.appendChild(div);
return document.getElementById(id);
}
function sendForm(form, url, func, arg) {
alert(form)
if (!document.createElement) return; // not supported
if (typeof(form)=="string") form=document.getElementById(form);
var frame=createIFrame();
frame.onSendComplete = function() { func(arg, getIFrameXML(frame)); };
form.setAttribute('target', frame.id);
form.setAttribute('action', url);
form.submit();
}
function sendComplete(id) {
var iframe=document.getElementById(id);
if (iframe.onSendComplete && typeof(iframe.onSendComplete) == 'function') iframe.onSendComplete();
}
function getIFrameXML(iframe) {
var doc=iframe.contentDocument;
if (!doc && iframe.contentWindow) doc=iframe.contentWindow.document;
if (!doc) doc=window.frames[iframe.id].document;
if (!doc) return null;
if (doc.location=="about:blank") return null;
if (doc.XMLDocument) doc=doc.XMLDocument;
return doc;
}
function uploadComplete(element, doc) {
if (!doc) return;
if (typeof(element)=="string") element=document.getElementById(element);
document.getElementById("limg").innerHTML = '';
element.innerHTML ='<img style="padding-left:10px" src="i/'+ doc.documentElement.firstChild.nodeValue +'">';
element.innerHTML += '<img src="i/x.png" onClick="delimg(\'dimg.cgi?file=\',\''+doc.documentElement.firstChild.nodeValue+'\',\'limg\')">';
}
function delimg(u,a,f) {
var xmlhttp = getXmlHttp()
xmlhttp.open('GET', u+a, false);
xmlhttp.send(null);
if(xmlhttp.status == 200) {
document.getElementById("resultDiv").innerHTML = '';
document.getElementById(f).innerHTML = '<input type=\'file\' name=\'uploadFile\' onChange="sendForm(this.form, \'uploadFile.cgi\', uploadComplete, \'resultDiv\');return true;">';
}
}
<div id="limg">
<form method="post" enctype="multipart/form-data" onsubmit="return valid_form1(this)" >
<input type="file" name="uploadFile" onChange="sendForm(this.form, 'uploadFile.cgi', uploadComplete, 'resultDiv');return true;"></div>
<div id="resultDiv" style="display:inline"></div>
</form>