2013-01-24 55 views
0

我以前问过这个问题,但我没有得到这个真正的解决方案。我正在开发一个社交网站。在用户注册页面,用户有规定添加一张照片。我的问题是我想要显示用户上传到div的图像,然后单击表单末尾的提交按钮。我搜索了很多,但无法找到正确的图像。显示图像,而上传

我的HTML代码是这样的:

<img src="<?php echo $row_picture;?>" name="picture" class="settingspic" width="75" height="91" alt="profile pic" name="picture"/><a href="" onclick="return uploadimg()"> Upload</a></li> 
<input type="file" name="file" id="file" style="display:none;" /> 

我的JavaScript代码是:

function uploadimg() 
{ 
var uploader = document.getElementById('file'); 
uploader.click(); 
return false; 
} 

当我点击提交按钮,图像插入database.I希望图像是当用户上传文件时显示。

+0

上传的文件。将它保存到临时的某个地方(数据库将工作)并将其设置为图像src。当使用单击“确定”时,将其保存到最终位置并将“src”更新为最终资源。 – 2013-01-24 08:00:29

+0

如果您需要在上传时显示图片,我认为您需要一些Flash脚本。否则,我不认为你可以使用JavaScript来访问本地文件来显示它。 –

+0

其实看来我错了。这个链接中有一些示例代码。 http://www.html5rocks.com/en/tutorials/file/dndfiles/它使用html5。 –

回答

0

这是我在我们的网站上使用的。这是为了在上传之前预览图像,即在提交表单之前。你的问题有点不同,但我希望这可以帮助。

HTML:

<img id="preview_img" style="max-width: 130px; max-height: 130px;" src="http://www.domain.com/images/<?php echo $this->profile_details->photo? $this->profile_details->photo: "default.png"; ?>"/> 
<input type="file" id="avatar" name="avatar" onchange="readURL(this);"/> 

的JavaScript:

function readURL(input) { 
    if (input.files && input.files[0]) { 
     var reader = new FileReader(); 

     reader.onload = function (e) { 
      $('#preview_img').attr('src', e.target.result); 
     } 

     reader.readAsDataURL(input.files[0]); 
    } 
}