无论扩展名是什么,JS 都显示照片

JS displaying photos no matter what the extension is

我的 JS 问题非常简单:我有一段代码可以读取和显示给定 PC 文件夹中的照片。问题是,我不知道如何让它显示所有类型的照片,而不考虑它们的扩展名。目前,它只显示 .jpg 文件。

我是这样写的:

var index = 1;
var tempImg = new Image();
tempImg.onload = function(){
    appendImg();
}
var loadImg = function(index){
    tempImg.src = 'img/' + index + '.jpg';
}
var appendImg = function(){
    var img = document.createElement('img');
    img.src = tempImg.src;

    document.body.appendChild(img);
    loadImg(index++);

}
loadImg(index);

如果您在本地执行此操作,则可以使用文件系统 api

https://www.html5rocks.com/en/tutorials/file/filesystem/

如果没有,您的文件夹为

0.jpg
1.gif
2.png
...

然后,如果您知道可以遇到的图像类型,则可以尝试每种图像

<style>
img { width:50px }
</style>
<script>
const folder = "img/"; 
const maxImages = 4; // I think there are at least 4 images in the folder
const suffix = ["jpg","gif","png"]; 
let cnt = 0, idx = 0, container, img;
const loadImage = () => {
  if (idx >= maxImages) return; 
  if (cnt ===0) img = new Image();
  img.onload=function() {
    var saveImg = document.createElement("img");
    saveImg.src = this.src;
    saveImg.title = folder+idx+"."+suffix[cnt];
    container.appendChild(saveImg);
    idx++;
    cnt=0; 
    console.log("new image");
    loadImage()
  }
  img.onerror = function() {
    console.log("failed to load",folder+idx+"."+suffix[cnt]);
    cnt++; // try next suffix
    if (cnt >= suffix.length) {
      console.log("gave up")
      var saveImg = document.createElement("img");
      saveImg.title = folder+idx+" not found";
      saveImg.src = 'data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wCEAAkGBxIQEBUQDxMVFRAVFRUQFQ4QEg8VFRAQFRIXGBURFRYYHSggGBonGxUWITEhJykrLi4uFx8zODMtNygtLisBCgoKDg0OGxAQGy0mHyYtLS0tLy0tLS0tKy0tLS0tLS0tLS0tLS0vLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLf/AABEIAOEA4QMBEQACEQEDEQH/xAAcAAEAAgMBAQEAAAAAAAAAA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fmbROniuVwscedhHG508gCh/5TLa3ti2c/VQ9tbCry7s8E9hYlmOpJJJPUknUmVHQSwsFvZWy78p/Dxq2sfyQE6epPQe8KLfYhZbCtZk8HVN1Pg4eVm0n9fstJ/67P6L+c2I0f8AY5V/U/Kpfizq2zdm041Yqx61rQdFQAe58zL0kuxyZzlN5k8luZIiAIAgCAIAgCAIAgCAIAgCAIAgCAIAgCAIBFk46WKUsUMh5FWAII+hgym08o5tvV8IaLtbMB/As6+C2rVMfId09tR6SmVKfY6VHUpx4s5X5nJN4d1svAbTJpZV6C0fNW30YcvzmvKDj3OtVqK7fCy6uHkZyY+FhVvYlKsWKj5fHtbisZm6cvlUeiiZw5YSK98KnKc3jP2R7zdj4M9LNo269/s1B/R7P6Ae8tjT6mjd1PyrX4s6nsrZVGLWK8apa0HZABr6k9zL0kuxy52Sm8yeS7MkBAEAQBAEAQBAEAQBAEAQBAEAQBAEAQBAEAQBAEAQDC6lXUq6hlPIqwBB9jBlNrsR4eHXSgrpRa0HRK1CgewmEsGZScnmTyTzJEQBAEAQBAEAQBAEAQBAEAQBAEAQBAEAQBAEAQBAEAQBAEAQBAEAQBAEAQBAEAQBAEAQBAEAQBAEAQBAEAQBAEAQBAEAQBAEAQBAEAQBAEAQBAEAQBAEAQBAEAQBAEAQBAEAQBAEAQBAEAQBAEAQBAEAQBAEAQBAEAQBAEAQD//Z';
      container.appendChild(saveImg);
      cnt=0; idx++;
    }
    loadImage();
  }
  console.log("trying to load", folder+idx+"."+suffix[cnt]);
  img.src = folder+idx+"."+suffix[cnt];
}

window.addEventListener("load",function() {
  container = document.getElementById("imagecontainer");
  loadImage();
});


</script>
<div id="imagecontainer"></div>

我假设你的图片是从1开始编号的。这使得只使用递增的索引号和硬编码的文件扩展名来加载图片成为可能,但正如您所意识到的,如果扩展名不是 .jpg,则会失败。

您可以做的是将包括扩展名在内的确切文件名存储在一个数组中,并使用索引号来引用其中的特定图片。

var index = 0;
var myPictures = ["1.jpg", "2.png", "3.png"];
var tempImg = new Image();
tempImg.onload = function() {
  appendImg();
}
var loadImg = function() {
  tempImg.src = 'img/' + myPictures[index];
}
var appendImg = function() {
  var img = document.createElement('img');
  img.src = tempImg.src;

  document.body.appendChild(img);
  if (index + 1 < myPictures.length) {
    index++;
    loadImg();
  }


}
loadImg();