您无法通过 JS 获取页眉,但您可以区分错误与成功:尝试这样的操作:
<script type="text/javascript">
var uploadStarted = false;
function OnUploadStart(){
uploadStarted = true;
}
function OnUploadComplete(state,message){
if(state == 1)
alert("Success: "+message);
else
if(state == 0 && uploadStarted)
alert("Error:"+( message ? message : "unknow" ));
}
</script>
<iframe id="uploader" name="uploader" onload="OnUploadComplete(0)" style="width:0px;height:0px;border:none;"></iframe>
<form id="sender" action="/upload.php" method="post" target="uploader" enctype="multipart/form-data" onsubmit="OnUploadStart()">
<input type="file" name="files[upload]"/>
<input type="submit" value="Upload"/>
</form>
在服务器端:
/*
file: upload.php
*/
<?php
// do some stuff with file
print '<script type="text/javascript">';
if(success)
print 'window.parent.OnUploadComplete(1,"File uploaded!");';
else
print 'window.parent.OnUploadComplete(0, "File too large!");';
print '</script>';
?>