如何随机获取一个文件名填入文本框

waaoo 2017-04-06 02:17:24
求高手帮忙,万分感谢

一个文件夹(比如 image/)中有大量的图片,名称是乱的,如何用JS或asp随机获取一个文件名,填入一个文本框中?

格式 【image/201612290925154846.jpg】

求代码!我只找到了可以获取到文件名列表的代码,但不知道如何利用获取到的文件名

<!doctype html>
<html>
<head>
<meta charset="gb2312">
<title>无标题文档</title>
</head>
<body>
<input type="text" name="name" id="name">
</body>
</html>

下面是获取列表的代码
<%
Dim fso, f, f1, fc, s
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.GetFolder(Server.MapPath("."))
Set files = f.Files
For Each file in files
%>
<a href="<%= file.Name %>"><%= file.Name %></a><br>
<%
Next
%>
...全文
321 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
waaoo 2017-04-14
  • 打赏
  • 举报
回复
引用 4 楼 masxq 的回复:
你这样做是没意义的,要让用户上传自己需要的文件
对我的使用来说是有意义的,用程序减少工作量,提高效率
浪子回头8 2017-04-12
  • 打赏
  • 举报
回复
你这样做是没意义的,要让用户上传自己需要的文件
waaoo 2017-04-07
  • 打赏
  • 举报
回复
引用 1 楼 cxc3214 的回复:
要么你就前后台分来,用jquery 的ajax请求,然后赋值, 要么就


<!doctype html>
<html>
<head>
<meta charset="gb2312">
<title>无标题文档</title>
</head>
<body>
<input type="text" name="name" id="name">
</body>
</html>

下面是获取列表的代码
<script>
function GetRandomNum(Min,Max)
{   
var Range = Max - Min;   
var Rand = Math.random();   
return(Min + Math.round(Rand * Range));   
}  

var arr = [];
<%
Dim fso, f, f1, fc, s
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.GetFolder(Server.MapPath("."))
Set files = f.Files
For Each file in files
%>
arr.push("<%= file.Name %>");
<%
Next
%>

var index = GetRandomNum(1,arr.length );
document.getElementById("name").value=arr[index];
</script>


非常感谢您的回复,测试成功,不过如何只提取 jpg png gif 这样的特定格式?现在随机时连这个html文件名都会进去。 另外能做成按顺序从文件列表中提出文件名后填入文本框中吗?一次获得全部文件名称后,每次刷新,按顺序从这个列表中得到一个文件名,直到最后一个,然后再从第一个文件名开始循环。
waaoo 2017-04-07
  • 打赏
  • 举报
回复


<!doctype html>
<html>
<head>
<meta charset="gb2312">
<title>无标题文档</title>
</head>
<body>
<input name="name" type="text" id="name" size="50">
</body>
</html>
 
下面是获取列表的代码
<script>
function GetRandomNum(Min,Max)
{   
var Range = Max - Min;   
var Rand = Math.random();   
return(Min + Math.round(Rand * Range));   
}  
 var arr = [];
<%
Dim fso, f, f1, fc, s
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.GetFolder(Server.MapPath("."))
Set files = f.Files
For Each file in files
%>
var file_name = "<%= file.Name %>";
var nReg = /\.jpg$|\.jpeg$|\.gif$|\.png$/i;
if (nReg.test(file_name)) {
	arr.push('/images/'+file_name);
};

<%
Next
%>
console.log(arr);
var index = GetRandomNum(0,(arr.length-1) );
document.getElementById("name").value=arr[index];
</script>

上面已解决了图片格式的问题,但如何获得列表后按顺序填入文本框?
cxc3214 2017-04-06
  • 打赏
  • 举报
回复
要么你就前后台分来,用jquery 的ajax请求,然后赋值, 要么就


<!doctype html>
<html>
<head>
<meta charset="gb2312">
<title>无标题文档</title>
</head>
<body>
<input type="text" name="name" id="name">
</body>
</html>

下面是获取列表的代码
<script>
function GetRandomNum(Min,Max)
{   
var Range = Max - Min;   
var Rand = Math.random();   
return(Min + Math.round(Rand * Range));   
}  

var arr = [];
<%
Dim fso, f, f1, fc, s
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.GetFolder(Server.MapPath("."))
Set files = f.Files
For Each file in files
%>
arr.push("<%= file.Name %>");
<%
Next
%>

var index = GetRandomNum(1,arr.length );
document.getElementById("name").value=arr[index];
</script>


28,390

社区成员

发帖
与我相关
我的任务
社区描述
ASP即Active Server Pages,是Microsoft公司开发的服务器端脚本环境。
社区管理员
  • ASP
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧