javascript 使用Javascript查找文件夹中的所有图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10127464/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 08:46:26  来源:igfitidea点击:

Find all images in folder with Javascript

javascriptjqueryimagefilepath

提问by Frank Visaggio

Sorry If this is trivial,

对不起,如果这是微不足道的,

Edit: in short I found out this really isn't intended to be done with client-side JavaScript.

编辑:简而言之,我发现这真的不打算用客户端 JavaScript 来完成。

I am wondering if i know a folder has pictures. /home/project1/pictures and they follow some naming convention face102039.jpg face1030303.jpg ...

我想知道我是否知道文件夹中有图片。/home/project1/pictures 并且它们遵循一些命名约定 face102039.jpg face1030303.jpg ...

Is there a was to know every picture thats in there starting with face but not knowing what numbers are after it? Like is there a way i could possibly grab all the file names with a .jpg or .png extension and then i would know how to parse their strings. I am just not sure how to get all the files in a directory with javascript.

有没有人知道那里的每张照片都以人脸开头,但不知道后面是什么数字?就像有没有办法我可以用 .jpg 或 .png 扩展名获取所有文件名,然后我会知道如何解析它们的字符串。我只是不确定如何使用 javascript 获取目录中的所有文件。

I saw this similar post, and I know how to do what I want in Java, but i couldnt find a way to do this in javascript. I was thinking about doing it server side with java or ruby but I am pretty sure i should be able to do this clientside via javascript. Maybe I am wrong though.

我看到了这个类似的帖子,我知道如何用 Java 做我想做的事,但我找不到在 javascript 中做到这一点的方法。我正在考虑用 java 或 ruby​​ 做服务器端,但我很确定我应该能够通过 javascript 来做这个客户端。也许我错了。

采纳答案by qw3n

Given the constraints it is technically possible to do this, just not very practical. I would not recommend using this since you could lock up the user's browser pretty quickly.

鉴于限制,在技术上可以做到这一点,只是不太实用。我不建议使用它,因为您可以很快锁定用户的浏览器。

var len = 2000000;//How long you want to wait.
var pics=[];
for(var i=0;i<len;i++){
  a=new Image();
  a.onload=function(){
    pics.push(this);
  }
  a.src='/home/project1/pictures/face'+i+'.jpg';
}

The real answer is you need to implement a back end in PHP, RoR, etc. To send you a list of the files in the picture folder. This can be done with AJAX.

真正的答案是您需要在 PHP、RoR 等中实现后端。向您发送图片文件夹中的文件列表。这可以通过 AJAX 来完成。

回答by Gert Van de Ven

Javascript is running at the client side. For example in the browser of the visitor. It has no direct access to the server and image folders, as Java and php does.

Javascript 正在客户端运行。例如在访问者的浏览器中。它不能像 Java 和 php 那样直接访问服务器和图像文件夹。

What can be done is using ajax in the javascript, to fetch a java/php file which lists the directory content.

可以做的是在 javascript 中使用 ajax 来获取一个列出目录内容的 java/php 文件。

回答by Jeff Hines

You can't access the file system with JavaScript. You could, however, use AJAXto query your server for that information and return it as a JSON string.

您无法使用JavaScript. 但是,您可以使用AJAX向服务器查询该信息并将其作为 JSON 字符串返回。

Hope this helps.

希望这可以帮助。