我有两个文件iframe.php
,iframe.html
. iframe.php
包含我想显示iframe.html
为 iframe 的文件上传表单(仅适用于 IE < 10,否则我可以使用 ajax 上传而不会出现任何问题)。
它在 IE.10 中工作正常,但在 IE.9 中我需要在提交按钮上单击两次才能上传,而在 IE.8 中它根本不起作用并且$output
总是返回Error: invalid file
并且还需要单击两次,我'不知道是什么问题!任何帮助都会很棒。
iframe.php
<div id="uploadImage">
<form method='POST' enctype='multipart/form-data' action='<?php echo $_SERVER['PHP_SELF'] ?>'>
<input type="text" disabled placeholder="Choose an Image to upload" id="file_input" name="file_input" />
<input type="button" id="file_btn" value="" onClick="document.getElementById('file').click()" />
<input type="submit" id="upload" value="" />
<div style="width: 0; height: 0; overflow: hidden;">
<input type="file" id="file" name="file" />
</div>
</form>
<div id="properties" class="texxt">
<p>Name: <br/>
Size: max 2 MB or 2000 KB<br/>
Type: (jpg, jpeg, png, gif)
</p>
</div>
<div id="results">
<?php
$status = "Close";
$source = "";
if( isset($_FILES['file']))
{
$allowedExts = array("gif", "jpeg", "jpg", "png");
$extension = end(explode(".", $_FILES["file"]["name"]));
$size = (int) $_SERVER['CONTENT_LENGTH'];
if ( (($_FILES["file"]["type"] == "image/gif") ||
($_FILES["file"]["type"] == "image/jpeg") ||
($_FILES["file"]["type"] == "image/jpg") ||
($_FILES["file"]["type"] == "image/png")) &&
in_array($extension, $allowedExts) &&
($size < (2 * 1024 * 1024)) )
{
if ($_FILES["file"]["error"] > 0)
{
$output = "Return Code: " . $_FILES["file"]["error"];
}
else
{
if (file_exists("upload/" . $_FILES["file"]["name"]))
{
$output = "Error: File already exists. Please rename your file or chose a different one";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],
"upload/" . $_FILES["file"]["name"]);
$status = "Add Image";
$output = "Uploaded Successfully";
$source = sprintf(" !img! %s !*img! ", $_FILES["file"]["name"]);
}
}
}
else
{
if (!in_array($extension, $allowedExts))
{
$output = "Error: Please select an image.";
}
elseif ( ($size > (2 * 1024 * 1024)) && in_array($extension, $allowedExts) )
{
$output = "Error: File size(" . $size . ") exceeds the limit of 2 mb.";
}
else
{
$output = "Error: Invalid file";
}
}
echo $output;
}
?>
</div>
<p align="center">
<input type="hidden" name="source" id="source" class="source" value="<?php echo $source ?>" />
<input type="button" id="close" class="close" value="<?php echo $status ?>" />
</p>
</div>
<script>
document.getElementById('file').onchange = function(){
var path = this.value.replace(/C:\\fakepath\\/, ''),
props = document.getElementById('properties');
props.innerHTML = props.innerHTML.replace('Name: ', 'Name: ' + path);
document.getElementById('file_input').value = path;
document.getElementById('results').innerHTML = "";
};
</script>
iframe.html
<div id="iframe" style="height: 296px; width: 481px">
</div>
<script>
var iframe = document.createElement('iframe');
iframe.src = "iframe.php";
iframe.frameBorder = "no";
iframe.allowTransparency = "true";
document.getElementById('iframe').appendChild(iframe);
</script>