解决:ASP判断文件后缀问题?

loveinet_168 2009-08-18 11:32:17
问题是这样的。
在网站里面做了个资料下载,在后台里上传了,比喻上传的是.RAR/.ZIP/.PDF等为后缀的文件。
在前台都可以调用出来,也可以显示,我想问大家的是,如何在前台页面中判断文件的后缀?
如果判断出来和上传的后缀是一致的就进行下载,如果判断的后缀不等于上传的就提示没有任何下载或没找到下载文件。
不知道如何做,想请教各位帮帮忙,如何解决?谢谢!!
...全文
397 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
loveinet_168 2009-08-21
  • 打赏
  • 举报
回复
谢谢各位,问题解决了.
Dogfish 2009-08-19
  • 打赏
  • 举报
回复
'现在的后缀名不一定是三位的。
suffix=""
ar = split(a, ".")
if ubound(ar)>0 then
suffix=ar(ubound(ar))
end if
Fiftyfive 2009-08-18
  • 打赏
  • 举报
回复

<%
'--------------------------------------------
Response.Buffer = True
Dim strFilePath, strFileSize, strFileName
Const adTypeBinary = 1
strFilePath = "文件路径 "
strFileSize = ... 文件大小,可选
strFileName = "文件名"
Response.Clear
'8*******************************************8
' 需要在你的服务器上安装 MDAC 2.6 或MDAC2.7
'8*******************************************8
Set objStream = Server.CreateObject("ADODB.Stream")
objStream.Open
objStream.Type = adTypeBinary
objStream.LoadFromFile strFilePath
strFileType = lcase(Right(strFileName, 4)) '文件扩展名
' 通过文件扩展名判断 Content-Types
Select Case strFileType
Case ".asf"
ContentType = "video/x-ms-asf"
Case ".avi"
ContentType = "video/avi"
Case ".doc"
ContentType = "application/msword"
Case ".zip"
ContentType = "application/zip"
Case ".xls"
ContentType = "application/vnd.ms-excel"
Case ".gif"
ContentType = "image/gif"

Case ".jpg", "jpeg"
ContentType = "image/jpeg"
Case ".wav"
ContentType = "audio/wav"
Case ".mp3"
ContentType = "audio/mpeg3"
Case ".mpg", "mpeg"
ContentType = "video/mpeg"
Case ".rtf"
ContentType = "application/rtf"
Case ".htm", "html"
ContentType = "text/html"
Case ".asp"
ContentType = "text/asp"
Case Else
'Handle All Other Files
ContentType = "application/octet-stream"
End Select
Response.AddHeader "Content-Disposition", "attachment; filename= strFileName
Response.AddHeader "Content-Length", strFileSize
Response.Charset = "UTF-8" ' 客户端浏览器的字符集UTF-8
Response.ContentType = ContentType
Response.BinaryWrite objStream.Read
Response.Flush
objStream.Close
Set objStream = Nothing
%>
hookee 2009-08-18
  • 打赏
  • 举报
回复

<script>
m = ".RAR/.ZIP/.PDF";

function doit(){
var f = document.forms[0];
var v = f.file1.value;
var p = v.lastIndexOf("\\");
v = v.substr(p+1, v.length-p);
p = v.lastIndexOf(".");
v = v.substr(p, v.length-p).toUpperCase();
var x = "/" + m + "/";
if(x.indexOf("/" + v + "/")==-1) alert("后缀不对");
else alert("OK")
}

</script>
<form>
<input type="button" value="click" onclick="doit()">
<input type="file" name="file1">
</form>
shenzhenNBA 2009-08-18
  • 打赏
  • 举报
回复
不是可以得到文件名的最后3个字符来判断吗?

<%
dim a=trim("xpath/xFile.yyy")
dim suffix
suffix=right(a,3)
if suffix="rar" then
'....xxx...
elseif suffix="zip" then
'....xxx...
elseif suffix="pdf" then
'....xxx...
end if
%>
街头小贩 2009-08-18
  • 打赏
  • 举报
回复
您的意思是:
若上传有aa.zip,而我下载时文件名为aa.exe就提示错误么?您可以取到文件名是吧!把点号(.)替换掉,比较两个字符串即可吧!
王者coco 2009-08-18
  • 打赏
  • 举报
回复
up
liuwei_IT_love 2009-08-18
  • 打赏
  • 举报
回复
无语了,2楼3楼比较简单实用
hanlongppl 2009-08-18
  • 打赏
  • 举报
回复
up

28,405

社区成员

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

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