Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/413.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/90.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从阵列60+中随机选取图像;选择javascript_Javascript_Html - Fatal编程技术网

从阵列60+中随机选取图像;选择javascript

从阵列60+中随机选取图像;选择javascript,javascript,html,Javascript,Html,我知道这个问题已经有很多答案了,但我正试图找出如何以最有效的方式解决这个问题。我有一个网站,通过点击按钮将图像发送到电话号码,但我想在60张左右的照片中进行选择,手动将所有这些图像位置输入阵列似乎并不理想。 这是我的js文件,它执行电子邮件操作,所有这些都托管在一个免费的托管服务上 //server.js const express=require(“express”) const app=express() 应用程序使用(快速静态(“公共”)) 应用程序获取(“/”,(请求,请求)=>{ re

我知道这个问题已经有很多答案了,但我正试图找出如何以最有效的方式解决这个问题。我有一个网站,通过点击按钮将图像发送到电话号码,但我想在60张左右的照片中进行选择,手动将所有这些图像位置输入阵列似乎并不理想。 这是我的js文件,它执行电子邮件操作,所有这些都托管在一个免费的托管服务上

//server.js
const express=require(“express”)
const app=express()
应用程序使用(快速静态(“公共”))
应用程序获取(“/”,(请求,请求)=>{
res.sendFile(uu dirname+“/index.html”)
/*当人们转到app.glitch.me/时,会发送“index.html”文件*/
})
app.get(“/send)”,(请求、回复)=>{
//节点应用程序的启动位置
var nodeEmailer=require('nodeEmailer');
var transporter=nodeEmailer.createTransport({
服务:“gmail”,
认证:{
用户:process.env.user,
通过:process.env.pass,
}
});
var mailpoptions={
发件人:process.env.USER,
收件人:process.env.RECIP,
主题:“根据您的要求”,
文本:“”,
附件:[
{
/*图像定位*/
路径:'https://post.healthline.com/wp-content/uploads/sites/3/2020/02/322868_1100-1100x628.jpg',
}
]
};
transporter.sendMail(邮件选项,函数(错误,信息){
如果(错误){
console.log(错误);
}否则{
console.log('发送的电子邮件:'+信息响应);
}
}); 
res.redirect(“/sent.html”)//发送电子邮件后,重定向回app.glitch.me上的“index.html”/
})
app.listen(3000)//通车
这是我的HTMl,如果它甚至与我的问题有关

<!DOCTYPE html>
<html lang="en">
  <head>
    <title>Hello!</title>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <!-- import the webpage's stylesheet -->
    <link rel="stylesheet" href="/style_index.css">

     <a href="/send">click me for snazzy pics</a><!-- script to ping --!>

    <!-- import the webpage's javascript file -->
    <script src="/script.js" defer></script>
  </head>  
  <body>
    <h1>hello</h1>

    <p>
      I made this.
    </p>
  </body>
</html>

你好
你好

这是我做的。


尝试首先记录所有图像的宿主位置。如果不是可以调用的数据库,则可能需要手动创建它们的数组。一旦它们位于对象中,就可以简单地使用变量来确定图像链接应该来自阵列中的哪个位置。我希望下面的内容能有所帮助

例如:


imageChoices = ["https://post.healthline.com/wp-content/uploads/sites/3/2020/02/322868_1100-1100x628.jpg", "https://post.healthline.com/wp-content/uploads/sites/3/2020/02/322868_1100-1100x628.jpg", etc.]

randomIndexChooser = Math.floor(Math.random() * 60) + 1;


var mailOptions = {
  from: process.env.USER,
  to: process.env.RECIP,
  subject: "As you requested",
  text: '',
  attachments: [
    {
 /*image location*/
      path: imageChoices[randomIndexChooser],
    }
  ]
};


尝试首先记录所有图像的宿主位置。如果不是可以调用的数据库,则可能需要手动创建它们的数组。一旦它们位于对象中,就可以简单地使用变量来确定图像链接应该来自阵列中的哪个位置。我希望下面的内容能有所帮助

例如:


imageChoices = ["https://post.healthline.com/wp-content/uploads/sites/3/2020/02/322868_1100-1100x628.jpg", "https://post.healthline.com/wp-content/uploads/sites/3/2020/02/322868_1100-1100x628.jpg", etc.]

randomIndexChooser = Math.floor(Math.random() * 60) + 1;


var mailOptions = {
  from: process.env.USER,
  to: process.env.RECIP,
  subject: "As you requested",
  text: '',
  attachments: [
    {
 /*image location*/
      path: imageChoices[randomIndexChooser],
    }
  ]
};


您需要创建一个调用api的ajax服务,api循环遍历指定文件夹中的所有文件并返回文件路径列表。从api中获取列表后,将它们附加到javascript代码中想要的数组中

我将在asp.net c#中为您提供一个示例,您可能正在使用另一个框架,但您至少可以从这个想法中受益

下面是api中的一个函数

[HttpGet]
公共列表getImageFilePath()
{
//getfiles返回指定目录中找到的所有文件的路径
List imagefilepath=Directory.GetFiles(“folderpath”、“*.png”、SearchOption.AllDirectories).ToList();
}
调用API的ajax服务

$.ajax({
url:“主机名/apiname/GetImageFilePath”
contentType:“应用程序/json”,
数据类型:“json”,
成功:功能(结果){
//在这里附加结果,即文件路径列表
//在你想要的数组中,你也可以循环
result.forEach((imagePath)=>{
arrayOfImages.push(图像路径)
})
}
})

您需要创建一个调用api的ajax服务,api将遍历指定文件夹中的所有文件,并返回文件路径列表。从api中获取列表后,将它们附加到javascript代码中想要的数组中

我将在asp.net c#中为您提供一个示例,您可能正在使用另一个框架,但您至少可以从这个想法中受益

下面是api中的一个函数

[HttpGet]
公共列表getImageFilePath()
{
//getfiles返回指定目录中找到的所有文件的路径
List imagefilepath=Directory.GetFiles(“folderpath”、“*.png”、SearchOption.AllDirectories).ToList();
}
调用API的ajax服务

$.ajax({
url:“主机名/apiname/GetImageFilePath”
contentType:“应用程序/json”,
数据类型:“json”,
成功:功能(结果){
//在这里附加结果,即文件路径列表
//在你想要的数组中,你也可以循环
result.forEach((imagePath)=>{
arrayOfImages.push(图像路径)
})
}
})

“手动将所有这些图像位置输入阵列似乎并不理想”。。。当然,它们必须列在某个地方,在源代码中包含数组是迄今为止最简单的。从该数组中随机选取一个条目是很简单的。我想可能有某种方法可以从图像文件夹中选取,而不是从数组@AKXThere中选取。当然有,但是如果所有图像都在远程主机上,就像您当前看到的那样,则没有图像文件夹可访问。“手动将所有这些图像位置输入阵列似乎并不理想"... 当然,它们必须列在某个地方,在源代码中包含数组是迄今为止最简单的。当你有一个数组时,从该数组中随机选取一个条目是很简单的。我想可能有某种方法可以从一个与数组@AKXThere相对的图像文件夹中选取。当然有,但是如果所有的图像都在一个远程主机上,就像你现在看到的那样,没有图像文件夹可以访问。好的,我会尝试将其移植到js。谢谢。好的,我会尝试把这个移植到js。非常感谢。