【问题标题】:Changing img src with the fileupload value using jquery/php/html使用 jquery/php/html 使用 fileupload 值更改 img src
【发布时间】:2013-10-10 08:39:37
【问题描述】:

我正在使用 html fileUpload 组件接受一些文本和图片的 facebook 选项卡,其想法是,当用户在 fileUpload 组件中选择图片时,他们选择的图像在页面上显示为在将图片上传到服务器之前预览图片的方式。我尝试使用 val() 获取图像 url,但出于安全原因,浏览器不会提供本地文件的完整 url。有没有办法使用 php 或 jquery 来做到这一点?

谢谢

【问题讨论】:

标签: php jquery image file-upload preview


【解决方案1】:

我相信this is what you're looking for

function handleFiles(files) {
  for (var i = 0; i < files.length; i++) {
    var file = files[i];
    var imageType = /image.*/;

    if (!file.type.match(imageType)) {
      continue;
    }

    var img = document.createElement("img");
    img.classList.add("obj");
    img.file = file;
    preview.appendChild(img);

    var reader = new FileReader();
    reader.onload = (function(aImg) { return function(e) { aImg.src = e.target.result; }; })(img);
    reader.readAsDataURL(file);
  }
}

您可能也有兴趣阅读其他示例,例如 this one

编辑:此方法依赖于FileReader API,因此正如您所指出的,它不适用于 IE9,但我认为否则不可能。
归根结底,您是否需要 IE9 兼容性?您可能会发现添加适用于除少数用户群之外的所有用户群的功能是可以接受的。

【讨论】:

  • 谢谢!由于项目的时间安排,这将不得不这样做,在除 IE9 之外的所有浏览器中都像魅力一样工作
猜你喜欢
  • 1970-01-01
  • 2012-08-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-11-26
  • 1970-01-01
  • 1970-01-01
  • 2022-01-15
相关资源
最近更新 更多