简体   繁体   English

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

[英]Find all images in folder with Javascript

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. 就像有一种方法,我可以用.jpg或.png扩展名获取所有文件名,然后我会知道如何解析他们的字符串。 I am just not sure how to get all the files in a directory with javascript. 我只是不确定如何使用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. 我看到了这个类似的帖子 ,我知道如何在Java中做我想要的,但我无法在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. 我正在考虑用java或ruby来做服务器端,但我很确定我应该能够通过javascript来做这个客户端。 Maybe I am wrong though. 也许我错了。

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. 真正的答案是您需要在PHP,RoR等中实现后端。向您发送图片文件夹中的文件列表。 This can be done with AJAX. 这可以通过AJAX完成。

Javascript is running at the client side. Javascript正在客户端运行。 For example in the browser of the visitor. 例如,在访问者的浏览器中。 It has no direct access to the server and image folders, as Java and php does. 它没有像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文件。

You can't access the file system with JavaScript . 您无法使用JavaScript访问文件系统。 You could, however, use AJAX to query your server for that information and return it as a JSON string. 但是,您可以使用AJAX在服务器中查询该信息,并将其作为JSON字符串返回。

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM