【问题标题】:Get SRC of image previewed in jquery获取在 jquery 中预览的图像的 SRC
【发布时间】:2013-06-16 02:02:12
【问题描述】:

我在做什么

我正在尝试在 php/jquery 中实现个人资料图片更改。上传图片时,会显示图片的预览。

我需要什么

我想获取预览图像的 src。当我使用$('img').attr('src'); 时,它会给出旧图像的 src,而不是新的预览图像。

脚本

function readURL(input) {
     if (input.files && input.files[0]) {
      var reader = new FileReader();
      reader.onload = function (e) {
      var image =  $('#img').attr('src', e.target.result);
      }
      reader.readAsDataURL(input.files[0]);
     }
     }
    $("#imgInp").change(function(){
      readURL(this);
     });

HTML

 <div class="pic">

  <form id="form1" enctype="multipart/form-data">

   <img src="profile.jpg" width="150"  alt="profile" title="" id="img"/>

     <span>Change Picture?</span>

      <div class="up">

         <input type='file' name ="file" id="imgInp"  />

     </div>

         <input type="submit" name="picture" id="picture" value="Update"/>

    </form>

 </div>

【问题讨论】:

标签: php jquery image-upload


【解决方案1】:

IE 8 中有一个新的安全“功能”,可以对 JavaScript 调用隐藏所选文件的真实路径(它返回 c:\fakepath)。这似乎只对“Internet”服务器激活......不是本地 Intranet 服务器......

See here 了解有关 C:\fakepath 的更多信息

编辑

另外,您应该考虑删除这行代码(您的脚本中不需要它)...

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-12-12
    • 2011-10-20
    • 2013-02-10
    • 2011-04-25
    • 1970-01-01
    • 2021-07-14
    • 1970-01-01
    • 2012-02-07
    相关资源
    最近更新 更多