File upload with Ajax
I have followed this example to upload file using AJAX. File is being uploaded successfully but the problem is stopUpload function is not invoked from uploadServer.php file. Can anyone tell me what I'm doing wrong? Here's my code -
Client side script - uploadfile.html
<html>
<body>
<style type="text/css">
#f1_upload_process{
z-index:100;
position:absolute;
visibility:hidden;
text-align:center;
width:400px;
margin:0px;
padding:0px;
background-color:#fff;
border:1px solid #ccc;
}
form{
text-align:center;
width:390px;
margin:0px;
padding:5px;
background-color:#fff;
border:1px solid #ccc;
}
</style>
<p id="f1_upload_process">Loading...<br/><img src="loader.gif" /></p>
<p id="resultp"></p>
<form action="http://aiworker2.usask.ca/uploadServer.php" method="post" enctype="multipart/form-data" target="upload_target" onsubmit="startUpload();" >
File: <input name="myfile" type="file" />
<input type="submit" name="submitBtn" value="Upload" />
</form>
<iframe id="upload_target" name="upload_target" src="#" style="width:0;height:0;border:0px solid #fff;"></iframe>
<script type="text/javascript">
function startUpload(){
document.getElementById('f1_upload_process').style.visibility = 'visible';
return true;
}
function stopUpload(success){
alert('in stopUpload: '+ success);
var result = '';
document.getElementById('f1_upload_process').style.visibility = 'hidden';
if (success == 1){
document.getElementById('resultp').innerHTML ='<span class="msg">The file was uploaded successfully!<\/span><br/><br/>';
}
else {
document.getElementById('resultp').innerHTML ='<span class="emsg">There was an error during file upload!<\/span><br/><br/>';
}
return true;
}
</script>
</body>
</html>
Server Side script - uploadServer.php
<?php
$destination_path = getcwd().DIRECTORY_SEPARATOR;
$result = 0;
$target_path = $destination_path . basename( $_FILES['myfile']['name']);
if(@move_uploaded_file($_FILES['myfile']['tmp_name'], $target_path)) {
$result = 1;
}
//sleep(1);
echo '开发者_开发问答<script language="javascript" type="text/javascript">';
echo 'window.top.window.stopUpload(\''.$result.'\');';
echo '</script>';
?>
Thanks
精彩评论