有没有使用FireReader预览图片上传(或伪造)的解决方案?
答案 0 :(得分:1)
您可以通过Ajax将文件发送到服务器(是的,这是可能的),然后让服务器将该临时文件的URL发回给您并在前端显示该文件。避免使用File API有点麻烦,但如果必须的话,这是我知道不依赖Flash的唯一方法...
请务必查看此plugin,即使只是为了获取灵感。
您还可以查看此thread详细说明其他许多方法。
希望这有帮助!
答案 1 :(得分:0)
我为我的大学项目编写了以下代码,并在我的本地服务器上正常工作。想法是使用iframes作为ajax无法处理二进制数据隐藏那些框架隐藏php表单按钮并使用虚假按钮进行浏览和上传文件(在这种情况下,如果用户按下上传按钮,则替换用户的个人资料图片。)避风港不包括删除或丢弃按钮的代码,因为它仍然在进行中但很容易,你可以运行ajax调用并调用一些PHP脚本来删除该临时文件。代码很大,包括所有javascript / jquery,php和html片段soplease问你是否有疑问。
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>
phpFile 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"];
}
&GT;
phpFile uploadphoto.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");
?>