Php 用Ajax上传文件
我遵循了使用AJAX上传文件的步骤。文件已成功上载,但问题是uploadServer.php文件中未调用stopUpload函数。谁能告诉我我做错了什么?这是我的密码- 客户端脚本-uploadfile.htmlPhp 用Ajax上传文件,php,javascript,ajax,file-upload,Php,Javascript,Ajax,File Upload,我遵循了使用AJAX上传文件的步骤。文件已成功上载,但问题是uploadServer.php文件中未调用stopUpload函数。谁能告诉我我做错了什么?这是我的密码- 客户端脚本-uploadfile.html <html> <body> <style type="text/css"> #f1_upload_process{ z-index:100;
<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>
#f1\u上传\u过程{
z指数:100;
位置:绝对位置;
可见性:隐藏;
文本对齐:居中;
宽度:400px;
边际:0px;
填充:0px;
背景色:#fff;
边框:1px实心#ccc;
}
形式{
文本对齐:居中;
宽度:390px;
边际:0px;
填充物:5px;
背景色:#fff;
边框:1px实心#ccc;
}
加载…
文件:
函数startupboad(){
document.getElementById('f1_upload_process')。style.visibility='visible';
返回true;
}
函数stopUpload(成功){
警报('在stopUpload中:'+成功);
var结果=“”;
document.getElementById('f1_upload_process')。style.visibility='hidden';
如果(成功==1){
document.getElementById('resultp').innerHTML='文件上载成功!
;
}
否则{
document.getElementById('resultp')。innerHTML='在文件上载过程中出错!
;
}
返回true;
}
服务器端脚本-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>';
?>
谢谢什么是“window.top.window”呢?你能把这个例子缩小到真正重要的部分吗?以目前的形式很难理解。