有没有在不使用 FireReader 的情况下预览图像上传(或伪造)的解决方案?
问问题
1708 次
2 回答
0
我为我的大学项目编写了以下代码,并且在我的本地服务器上运行良好。想法是使用 iframe,因为 ajax 无法处理二进制数据隐藏那些框架隐藏 php 表单按钮并使用假按钮浏览和上传文件(在这种情况下,如果用户按下上传按钮,请替换用户的个人资料图片)。我没有'不包括删除或丢弃按钮的代码,因为它仍在进行中,但它很容易,您可以运行 ajax 调用并调用一些 php 脚本来删除该临时文件。代码很大,包括所有 javascript/jquery、php 和 html 片段,如果您有任何疑问,请询问。
Javascript代码
var photoname;
$('#profilepic ').delegate('button#uploader1','click',function () {
if ($(this).prop("type") == "button") {
$('#profilepic').attr('target','hiddenframe1').attr('action','/phpScripts/tempphoto.php');
$('#uploadphoto').removeAttr('src');
$('#profilepic input#photo').click();
$(this).remove();
$('<button type="submit" id="uploader2">Upload</button>').insertBefore($('#profilepic button#removephoto'));
setTimeout(function () {
var interval = setInterval(function () {
if ($('#photo').val()) {
photoname = $('#photo').val().substring($('#photo').val().lastIndexOf('\\') + 1)
$('#profilepic input#submit').trigger('click', function (event) {})
setTimeout(function(){
$('#profilepic').css(
{
backgroundImage: 'url(/data/' + usr +'/'+photoname + ')'
})
},1000);
clearInterval(interval);
}
}, 200);
}, 200);
}
})
$('#profilepic ').delegate('#uploader2','click',function (){
if ($(this).prop("type") == "submit") {
$('#profilepic').attr('target','hiddenframe2').attr('action','/phpScripts/uploadphoto.php');
$('#uploadphoto').attr('src','/phpScripts/uploadphoto.php');
$('#profilepic input#submit').click();
$(this).remove();
$('<button type="button" id="uploader1">Browse</button>').insertBefore($('#profilepic button#removephoto'));
document.getElementById('profilepic').reset();
}
})
页面第一次加载或刷新时的 html 文件
<form id="profilepic" target="hiddenframe1" action="/phpScripts/tempphoto.php" method="post" enctype="multipart/form-data" style="background-image: url(http://www.xyz.com/data/UDB/UDB.jpg); width: 180px; height: 180px; background-size: cover; background-position: 50% 50%; background-repeat: no-repeat no-repeat;">
<input type="file" name="photo" id="photo" style="display: none;">
<input type="submit" name="submit" value="Submit" id="submit" style="display: none;">
<button type="button" id="uploader1" style="display: none;">Browse</button>
<button type="button" id="removephoto" style="display: none;">Remove</button>
</form>
<iframe id="tempphoto" name="hiddenframe1" src="/phpScripts/tempphoto.php"></iframe>
<iframe id="uploadphoto" name="hiddenframe2"></iframe>
php文件 tempphoto.php
<?php
session_start();
$dt=$_SESSION['uname'];
$dest=$_SERVER['DOCUMENT_ROOT']."/data/$dt/";
$filename;
if ($_FILES["photo"]["error"] > 0)
{
echo "Error: " . $_FILES["photo"]["error"] . "<br>";
}
else
{
echo "Upload: " . $_FILES["photo"]["name"] . "<br>";
echo "Type: " . $_FILES["photo"]["type"] . "<br>";
echo "Size: " . ($_FILES["photo"]["size"] / 1024) . " kB<br>";
echo "Stored in: " . $_FILES["photo"]["tmp_name"];
move_uploaded_file($_FILES['photo']['tmp_name'], $dest.$_FILES['photo']['name']);
$filename=$_FILES["photo"]["name"];
}
?>
php文件上传photo.php
<?php
session_start();
$dt=$_SESSION['uname'];
$dest=$_SERVER['DOCUMENT_ROOT']."/data/$dt/";
$filename;
if ($_FILES["photo"]["error"] > 0)
{
echo "Error: " . $_FILES["photo"]["error"] . "<br>";
}
else
{
echo "Upload: " . $_FILES["photo"]["name"] . "<br>";
echo "Type: " . $_FILES["photo"]["type"] . "<br>";
echo "Size: " . ($_FILES["photo"]["size"] / 1024) . " kB<br>";
echo "Stored in: " . $_FILES["photo"]["tmp_name"];
move_uploaded_file($_FILES['photo']['tmp_name'], $dest.$_FILES['photo']['name']);
$filename=$_FILES["photo"]["name"];
}
unlink($dest.$dt.".jpg");
rename($dest.$filename,$dest.$dt.".jpg");
?>
于 2013-10-17T16:54:25.073 回答