简体   繁体   English

将图片放入javascript中的数组

[英]getting pictures into an array in javascript

im kinda new to javascript i mean i know the syntax but not so much the libraries. 我对javascript有点陌生,我的意思是我知道语法,但对库却了解不多。

i need some help to get some files (pictures) from a folder into an arry lets say: 我需要一些帮助,以将文件夹中的某些文件(图片)放入arry中,让我们说:

var[] pictures = ?;

(the folder is in my project and contain some pictures) (该文件夹在我的项目中,其中包含一些图片)

so i can loop over them and diplay them on the page i did some search but i didnt find any guide on how to do this. 所以我可以遍历它们,并在页面上显示它们,我进行了一些搜索,但是我没有找到任何有关操作方法的指南。

i realy want to understand on how to do this for future projects even a link to known guid you guys know we will be a big help. 我真的很想了解如何在将来的项目中做到这一点,甚至链接到已知的GUID,你们都知道,我们将为您提供很大的帮助。

if it help im using asp.net. 如果它帮助即时通讯使用asp.net。

Well, there are a lot of ways to approach the problem, to me what you can do is (if you don't know the location of the images beforehand) make a service that returns the src of every image, store that in an array, and then show them in the page. 嗯,有很多方法可以解决该问题,对我来说,您可以做的是(如果您事先不知道图像的位置)创建一个返回每个图像的src的服务,并将其存储在数组中,然后在页面中显示它们。

I believe you are using jQuery so you can make an ajax request like this: 我相信您正在使用jQuery,因此您可以发出这样的ajax请求

jQuery.ajax({
     url: /*path to*/"Service.asmx/getSources"
     //options, check documentation
});

then, from asp, make a new service ( Service.asmx in my case) and create a method that returns the location of the pictures (in my case the method is called getSources ) 然后,从ASP中创建一个新服务(在我的情况下为Service.asmx ),并创建一个返回图片位置的方法(在我的情况下,该方法称为getSources

I recommend you use JSON (and jQuery.getJSON() method) so you can return a List<string> . 我建议您使用JSON(和jQuery.getJSON()方法),以便您可以返回List<string>

Lastly you can iterate or store the sources in an array, I'll put an example with the getJSON method 最后,您可以将源迭代或存储在数组中,我将使用getJSON方法举一个示例

var sources = []
jQuery.getJSON("Service.asmx/getSources", function(data) {
    for(var i = 0, len = data.length; i<len ; i++) {
      sources.push(data[i]);//store every source in the array
    }
});

once you have the sources you can display them like this fiddle 一旦有了来源,就可以像这样摆弄它们来显示它们

Tell me if it helped or if you need another solution. 告诉我它是否有帮助,或者您是否需要其他解决方案。

If you want an array of pictures just to display them later, you can simply use: 如果您只想显示一组图片以便以后显示,则可以使用:

var sources = [
    "path/to/yourImage1.jpg",
    "path/to/yourImage2.jpg",
    // ...
    "path/to/yourImageN.jpg",
];

var pics = [];
for(var i = 0; i < sources.length; i++) {
    var pic = new Image();
    pic.src = sources[i];
    pics[i] = pic;
}

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

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