Javascript HTML文件上传-为什么使用IFrame
我一直试图让文件上传在IE8中工作。我看到的唯一解决方案是发布到IFrame。为什么要这样做?不可能有一个简单的表格吗Javascript HTML文件上传-为什么使用IFrame,javascript,php,html,iframe,Javascript,Php,Html,Iframe,我一直试图让文件上传在IE8中工作。我看到的唯一解决方案是发布到IFrame。为什么要这样做?不可能有一个简单的表格吗 <form action="test.php" method="post" enctype="multipart/form-data"> Select image to upload: <input type="file" name="fileToUpload" id="fileToUpload"> <input type=
<form action="test.php" method="post" enctype="multipart/form-data">
Select image to upload:
<input type="file" name="fileToUpload" id="fileToUpload">
<input type="submit" value="Upload Image" name="submit">
</form>
为什么需要IFrame
谢谢iframe方法非常简单。 基本上,您使用iframe来上载文件,而不是使用主窗口来上载文件,但不需要使用iframe 方法1 这是关于该主题的一个很好的教程: HTML: PHP: 2。AJAX方法 JS: HTML:
文件上传
上传文件不需要iframe
您需要一个iframe来上传文件,而不离开当前页面(即Ajax)。现代浏览器支持FormData
,允许您使用XMLHttpRequest
上传文件
if(isset($_POST["submit"])) {
$check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
if($check !== false) {
echo "File is an image - " . $check["mime"] . ".";
$uploadOk = 1;
} else {
echo "File is not an image.";
$uploadOk = 0;
}
}
<form id="my_form" name="form" action="upload.php" method="POST"
enctype="multipart/form-data" >
<div id="main">
<input name="my_files" id="my_file" size="27" type="file" />
<input type="button" name="action" value="Upload" onclick="redirect()"/>
<iframe id='my_iframe' name='my_iframe' src="">
</iframe>
</div>
</form>
function redirect()
{
//'my_iframe' is the name of the iframe
document.getElementById('my_form').target = 'my_iframe';
document.getElementById('my_form').submit();
}
$uploaddir = '/images/';
$uploadfile = $uploaddir . basename($_FILES['my_files']['name']);
if (move_uploaded_file($_FILES['my_files']['my_name'], $uploadfile)) {
echo "success";
} else {
echo "error";
}
function submitForm() {
var formData = new FormData($('#imageForum')[0]);
$.ajax({
url: '/FileUpload',
type: 'POST',
data: formData,
async: false,
success: function (data) {
alert('posted')
},
cache: false,
contentType: false,
processData: false
});
return false;
}
<form id="imageForum" action="javascript:submitForm();" method="post" enctype = "multipart/form-data">
<div>
<label for="fileUpload">File upload</label>
<input type="file" id="fileUpload" name="fileUpload" />
</div>
</form>