AJAX style file upload without page refresh
July 13th, 2009 in PHP, Web UI Programming. 8 commentsHere’s a quick and dirty How-To on creating an AJAX type file upload without refreshing the html page. It is not a very elegant solution but does work well across browsers. Although the server side code shown here uses PHP, this solution can be adapted for Java, Perl or whatever else you use on the server.
-
First the html. We define a form and an iframe target with width and height set to 0 in order to keep it hidden. A message to display the upload status is also defined (within the span tags) and is initially hidden.
<form id="uploadform" method="post" enctype="multipart/form-data" action="lib/upload.php"> <input name="file" id="file" size="27" type="file" /><br /> <input type="submit" name="action" value="Upload" /> <span id="status" style="display:none">uploading...</span> <iframe id="target_iframe" name="target_iframe" src="" style="width:0;height:0;border:0px"></iframe> </form>
- Next the javascript. Explanation is inline:
<script type="text/javascript">
function initUpload() {
/*set the target of the form to the iframe and display the status
message on form submit.
*/
document.getElementById('uploadform').onsubmit=function() {
document.getElementById('uploadform').target = 'target_iframe';
document.getElementById('status').style.display="block";
}
}
//This function will be called when the upload completes.
function uploadComplete(status){
//set the status message to that returned by the server
document.getElementById('status').innerHTML=status;
}
window.onload=initUpload;
</script>
So how is uploadComplete called? keep reading….
Also you need not use php for the serverside script, you can substitute php with any other language, as long as your returns the same string (or something equivalent) as that being returned from returnStatus.
<?php
$file_id='file';
$status='';
$filename=$_FILES[$file_id]['name'];
$tmpfile=$_FILES[$file_id]['tmp_name'];
if(!$_FILES[$file_id]['name']) {
echo returnStatus("<font color=\'red\'>no file specified</font>");
return;
}
/*copy file over to tmp directory */
if(move_uploaded_file($tmpfile, "tmp/".$filename)){
$status='Success';
}else{
$status='<font color=\'red\'>Failed</font>';
}
echo returnStatus($status);
function returnStatus($status){
return "<html><body><script type='text/javascript'>function init(){if(top.uploadComplete) top.uploadComplete('".$status."');}window.onload=init;
</script></body></html>";
}
?>

Ajax style upload without page refresh
Related articles
Create a html table with thin borders
Jump to a different location on the same HTML page on dropdown list selection
Tags: html, javascript, PHP

(2 votes, average: 4.00 out of 5)
July 23rd, 2009 at 8:32 am says:
Correct these mistakes please:
At HTML [the quotes at style property]
and Ajax:
document.getElementById(’uploadform’).onsubmit=function()
Regards,
El
July 26th, 2009 at 6:54 pm says:
Thanks for pointing this out..The two errors have been recitified.
August 26th, 2009 at 7:40 pm says:
Hi…
I can’t do this example to work (sorry for my bad english)…
Is there possivel do you the complete files por download??
Thanks,
Antonio
August 27th, 2009 at 7:58 pm says:
Antonio,
Can you be more specific as to what errors you are getting?
All the code is already there above. Just cut and paste. Put the html and javascript in one html file and put the php code on your server side (in lib/upload.php). Thats it.
September 10th, 2009 at 1:48 am says:
Hi.
It always gives me “no files specified” everytime I click on the upload button.
September 24th, 2009 at 8:31 am says:
You need to put a valid action atrribute in the form tag.
September 25th, 2009 at 6:27 am says:
The action attribute is valid. In this case the php file is put inside the lib directory on the server.
February 20th, 2010 at 4:38 am says:
I Really Worry about how to make file upload to my server which is in .Net from html page without page refresh