首页 > 伪编程 > 工作小记:图片自动判断更新

工作小记:图片自动判断更新

2012年12月20日 437,545 浏览数 发表评论 阅读评论

  事情是这样的:有一个页面要展示几张图片,这几张图片分别来自不同的地方,而且图片的路径随日期变化,但是图片之间并不是统一更新的。当访问者看到这个页面的时候,要根据当天的日期看到最新的图片内容。

  操作如下:

  将已知的图片路径和日期保存到文本文件中如:epic.txt 格式为:2012-12-10|http://www.wuhuaguo.org/wp-content/2012-12/10/gl.jpg 一行一条

页面主文件index.asp:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
<%
'打开epic.txt文件,并将内容一行行读进str_e数组中
 
dim file,fso,path,I
Set fso = Server.CreateObject("scripting.FileSystemObject")
path = Server.MapPath("epic.txt")
set file = Fso.OpenTextFile(path,1,TRUE)
 
dim str_e(7),str(7),date_e(7)
I=0
do while not file.AtEndOfStream
  str_e(I)=file.readLine()
  I=I+1
loop
file.Close
 
'格式化日期,并生成图片路径地址
 
dim datetime,datetime1,datetime2
datetime=FormatMyDate(now,"{Y}-{m}-{d}")
datetime1=FormatMyDate(now,"{Y}-{m}/{d}")
datetime2=FormatMyDate(now,"{Y}{m}{d}")
 
'图1
str(0)="http://www.wuhuaguo.org/page/1/"&datetime1&"/01/"&datetime2&"01_brief.jpg" 
'图2
str(1)="http://www.wuhuaguo.org/page/219/"&datetime1&"/A01/"&datetime2&"A01_brief.jpg" 
'图3
str(2)="http://www.wuhuaguo.org/page/84/"&datetime1&"/A01/"&datetime2&"A01_brief.jpg" 
'图4
str(3)="http://www.wuhuaguo.org/page/501/"&datetime1&"/01/"&datetime2&"01_brief.jpg"
'图5
str(4)="http://www.wuhuaguo.org/page/224/"&datetime1&"/01/"&datetime2&"01_brief.jpg"
'图6
str(5)="http://www.wuhuaguo.org/page/234/"&datetime1&"/01/"&datetime2&"01_brief.jpg"
'图7
str(6)="http://www.wuhuaguo.org/page/233/"&datetime1&"/01/"&datetime2&"01_brief.jpg"
 
'判断图片是否存在,如果存在就替换掉数组中的老地址
For I=0 to 6
  date_e(I)=FormatMyDate(now,"{Y}-{m}-{d}")
  if not CheckURL(str(I)) then
    str(I)=right(str_e(I),len(str_e(I))-11)
    date_e(I)=left(str_e(I),10)
  end if  
Next
%>
<html>
<head>
<title>图片自动更新演示</title>
</head>
 
<body>
下面就是图片使用了:
<a href="<%=str(0)%>" target="_blank"><img src="<%=str(0)%>" border="0" >固定的图片名称 (<%=date_e(0)%>)</a>
<a href="<%=str(1)%>" target="_blank"><img src="<%=str(1)%>" border="0" >固定的图片名称 (<%=date_e(1)%>)</a>
... ...
</body>
</html>
<%
'上述代码用到的两个函数:
 
Function CheckURL(byval A_strUrl)
    set XMLHTTP = Server.CreateObject("Microsoft.XMLHTTP")
    XMLHTTP.open "HEAD",A_strUrl,false
    XMLHTTP.send()
    CheckURL=(XMLHTTP.status=200)
    set XMLHTTP = nothing
End Function
 
Function FormatMyDate(myDate,Template)
        If Not IsDate(myDate) Or Template = "" Then
            FormatMyDate = Template
            Exit Function
        End If
 
        Dim mYear,mMonth,mDay,mHour,mMin,mSec
            mYear = Year(myDate)
            mMonth = Month(myDate)
            mDay = Day(myDate)
            mHour = Hour(myDate)
            mMin = Minute(myDate)
            mSec = Second(myDate)
 
        Template = Replace(Template,"{Y}",Year(myDate))
        Template = Replace(Template,"{y}",Right(Year(myDate),2))
        Template = Replace(Template,"{M}",Month(myDate))
        Template = Replace(Template,"{m}",Right("00" & Month(myDate),2))
        Template = Replace(Template,"{D}",Day(myDate))
        Template = Replace(Template,"{d}",Right("00" & Day(myDate),2))
        Template = Replace(Template,"{H}",Hour(myDate))
        Template = Replace(Template,"{h}",Right("00" & Day(myDate),2))
        Template = Replace(Template,"{MI}",Minute(myDate))
        Template = Replace(Template,"{mi}",Right("00" & Minute(myDate),2))
        Template = Replace(Template,"{S}",Second(myDate))
        Template = Replace(Template,"{s}",Right("00" & Second(myDate),2))
 
        FormatMyDate = Template
End Function
%>

简述:

  用户访问时,先打开文本文件读取所有图片地址保存在数组中,然后根据最新日期判断图片是否更新,如果更新了则将新地址替换数组中的老地址。

———————————–我是快乐的分隔符————————————————

  我们还得需要一个更新过程,每隔一定时间自动判断一下图片是否更新,然后把结果保存在epic.txt文件中,以供index.asp调用。

自动更新epic.txt内容的save.asp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
<%
'//保存最新图片链接到文本文件中
 
dim file,fso,path,I,str_txt
Set fso = Server.CreateObject("scripting.FileSystemObject")
path = Server.MapPath("epic.txt")
set file = Fso.OpenTextFile(path,1,TRUE)
 
dim str_e(7),str(7),date_e(7)
I=0
do while not file.AtEndOfStream
  str_e(I)=file.readLine()
  I=I+1
loop
file.Close
'for i=0 to 6
    'response.write ">>"&I&":"&str_e(I)&"<<<br>"
'next
 
dim datetime,datetime1,datetime2
datetime=FormatMyDate(now,"{Y}-{m}-{d}")
datetime1=FormatMyDate(now,"{Y}-{m}/{d}")
datetime2=FormatMyDate(now,"{Y}{m}{d}")
 
'图1
str(0)="http://www.wuhuaguo.org/page/1/"&datetime1&"/01/"&datetime2&"01_brief.jpg" 
'图2
str(1)="http://www.wuhuaguo.org/page/219/"&datetime1&"/A01/"&datetime2&"A01_brief.jpg" 
'图3
str(2)="http://www.wuhuaguo.org/page/84/"&datetime1&"/A01/"&datetime2&"A01_brief.jpg" 
'图4
str(3)="http://www.wuhuaguo.org/page/501/"&datetime1&"/01/"&datetime2&"01_brief.jpg"
'图5
str(4)="http://www.wuhuaguo.org/page/224/"&datetime1&"/01/"&datetime2&"01_brief.jpg"
'图6
str(5)="http://www.wuhuaguo.org/page/234/"&datetime1&"/01/"&datetime2&"01_brief.jpg"
'图7
str(6)="http://www.wuhuaguo.org/page/233/"&datetime1&"/01/"&datetime2&"01_brief.jpg"
 
For I=0 to 6
  date_e(I)=FormatMyDate(now,"{Y}-{m}-{d}")
  if not CheckURL(str(I)) then
    str(I)=right(str_e(I),len(str_e(I))-11)
    date_e(I)=left(str_e(I),10)
  end if 
  str_txt=str_txt&date_e(I)&"|"&str(I)& vbcrlf
Next
 
'开始保存
path = Server.MapPath("epic.txt")
set file = Fso.OpenTextFile(path,2,TRUE)
  file.write str_txt
file.Close
set file=nothing
set Fso=nothing
 
'///////////////////////////////////
 
Function CheckURL(byval A_strUrl)
    '已经提供,参照index.asp
End Function
 
Function FormatMyDate(myDate,Template)
     '已经提供,参照index.asp
End Function
%>
<script language=javascript> 
window.opener=''; 
window.open('','_self');
window.close(); 
</script>

简述:其实这个文件只比index.asp多了个保存过程,完全可以合并到index.asp中,我这将代码重新帖一遍加深印象,哈哈

———————————我是快乐的分隔符———————————

  自动更新的程序已经搞定了,总不能让我人工定时去点击它吧?所以,要让它自动运行。比较的简单的办法是:利用操作系统中的“计划任务”来自动运行save.asp文件。代码如下:

1
2
3
4
5
Dim IE 
Set IE = CreateObject("InternetExplorer.Application") 
IE.navigate("http://www.wuhuaguo.org/save.asp") 
IE.visible=1 
Set IE = Nothing

将上述代码保存为AutoRun.vbs文件,然后在“计划任务”里设定时间让它自动运行即可。
注:自动运行时会打开Ie浏览器窗口但无法关闭,所以save.asp底下的JS代码就是由自动关闭窗口用的。

分类: 伪编程 标签: ,
  1. 本文目前尚无任何评论.