Iframe将不显示图像
Iframe will not display image
我使用隐藏的iframe发布用户从本地磁盘中选择的照片。然后,我尝试在与隐藏iframe相同的页面上的div上显示该图像。它没有在div中显示图像,我花了几个小时处理它
这是HTML:
<iframe name="upload_iframe" id="upload_iframe" style="display:none"></iframe>
<form name="pictureForm" method="post" autocomplete="off"
enctype="multipart/form-data">
<div>
<span>Upload Picture</span>
<input type="file" name="picture" id="picture"
onchange="return FileUpload(this);" />
<!-- this div is where I need the uploaded image to show....-->
<div id="picture_preview"></div>
</div>
</form>
当用户选择一个图像文件时,您可以在上面看到我的onchange
调用FileUpload(this)
——这里是FileUpload(),一个javascript函数:
function FileUpload(the_upload_field)
{
// show a 'progress bar' image whilst we do the image file upload
// -- this works fine.
document.getElementById('picture_preview').innerHTML
= '<div><img src="images/progressbar.gif" border="0" /></div>';
// now post the image to the server using the hidden iframe
the_upload_field.form.action = 'photoPreview.php';
the_upload_field.form.target = 'upload_iframe';
the_upload_field.form.submit();
the_upload_field.form.action = '';
the_upload_field.form.target = '';
return true;
}
您可以看到,我正在将这个隐藏的iframe发布到photoPreview.php——这是代码:
$upload_dir = 'file-upload/upload/'; // Directory for file storing
$preview_url = 'http://localhost/myWebsite/file-upload/upload/';
// NOTE: the 'picture' here is the name of the open-file html element above
$filename = $_FILES['picture']['name'];
$targetDirForFile = $upload_dir . $filename;
// extract the uploaded file and put it into the web server upload folder...
move_uploaded_file($_FILES['picture']['tmp_name'], $targetDirForFile);
// EDIT: this was in my code but forgot to include it here, now done so.
$fullImagePath = $preview_url . $filename;
// This is PHP code outputing Javascript code.
echo '<script type="text/javascript">'."'n";
//echo 'var parDoc = window.parent.document;'; // didn't work either.
echo 'var parDoc = parent.document;';
// if you look in the html code above, you see that 'picture_preview'
// used here is a div, and that's where I need the image to appear
$outStr = "parDoc.getElementById('picture_preview').innerHTML = '<div><img src=''$fullImagePath'' /></div>';";
// display the image to the 'picture_preview' div on the same page as the
// hidden iframe...
// EDIT: THIS WAS THE SOURCE OF MY PROBLEM. This showAlertBox() is my homemade
// helper function that displays PHP variables to the browser when I'm debugging.
// If you look at my code below for 'showAlertBox()' you see that it prematurely
// was ending the 'javascript' section of the code due to its own final '/script'
// WHEN I COMMENT OUT this call to showAlertBox(), my image now successfully appears.
showAlertBox("the outStr is" . $outStr);
echo $outStr;
echo "'n".'</script>';
exit();
我在picture_previewdiv中看到了进度条.gif,但没有看到上传的图像。
这应该有效,可能是我没有看到的简单的事情,我已经尝试了10个小时了。
此外,在上面的表单中移动iframe也没有任何区别。
编辑:我解决了这个问题。这就是我的问题的原因,我的showAlertBox()辅助函数,它在我调试PHP代码时向浏览器吐出PHP变量,并且它有一个尾部/脚本,阻止了我的最后一行代码"echo-outStr"执行和显示图像:
function showAlertBox($messageToDisplay)
{
echo '<script type="text/javascript">'
. 'alert("' . $messageToDisplay . '")</script>';
}
我修复了它。我有一个额外的"script"标记,它在我的最后一个"echo-outStr"语句之前过早地关闭了javascript代码。它没有出现在我的代码中,因为它来自一个名为showAlertBox()的"助手"函数,我在所有PHP开发中都使用它来在浏览器窗口中显示PHP变量的值——我的showAlertBox()使用以"script"开头的javascript,一个"alert(somePHPvariable)",然后是一个结束的"script"——这"关闭"了我最后一行本应显示图像的"echo-outStr"代码。我取消了showAlertBox()调用,瞧,它能在中工作
- 如何使用url加载程序在webpack中导入多个图像
- nodejs-expressjs上传图像并显示它们
- 使用JSP从服务器检索和显示图像
- 角度:在ng重复上切换图像
- 画廊图像未显示
- Phonegap-(安卓/iphone)多个图像的图像库出现问题
- Javascript排序的图像弹出窗口..可以't单独弹出
- 画布数据到图像
- Windows8应用程序(html&Javascript):从图片库(除了文件选择器)显示图像的另一种方式
- 使图像在单击时展开到不大于浏览器
- Html页面上的多个Base64图像和平滑加载
- UIAutomation放大图像
- 使用javascript在Flash中加载外部图像
- 可以在这里为背景图像设置滤镜吗
- 当图像转换为本地存储的DataURL时,EXIF被删除
- 使用 js 将背景图像设置为 HTML
- Javascript将图像src更改为淡入淡出
- 无法在本地计算机中将画布另存为图像
- 压缩phonegap中ios的图像插件
- 如何在鼠标悬停时在另一个图像上滑动图像.