js上传文件格式限制
❶ 怎么限制上传文件格式
在源代码里面该嘛
给你一段你照抄
UpFile.asp文件
<!--#include file="conn.asp"-->
<%
'这里是接受COOKice
username=session("username")
passwd=session("Passwd")
%>
<%
' 整个网站的安全要紧啊!如果没登陆那就给我到Login.asp页面去吧!
Set rs = Server.CreateObject("ADODB.Recordset")
sql="select * from admin where username='"&username&"'and passwd='"&passwd&"'"
rs.open sql,conn,1,1
if rs.eof then
response.redirect "login.asp"
response.end
end if
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<script language="javascript">
function mysubmit(theform)
{
if(theform.Text_Files.value=="")
{
alert("请点击浏览按钮,选择您要上传的jpg或gif文件!")
return (false);
}
else
{
str= theform.Text_Files.value;
strs=str.toLowerCase();
lens=strs.length;
extname=strs.substring(lens-4,lens);
if(extname!=".jpg" && extname!=".gif")
{
alert("请选择jpg或gif文件!");
return (false);
}
}
return (true);
}
</script>
</head>
<body>
<table width="293" border="0" cellspacing="0">
<tr>
<td> <form name="FileForm" method="post" enctype="multipart/form-data" action="Save_UpFiles.asp" onsubmit="return mysubmit(this)">
<input name="Text_Files" type="file" style="width:80%">
<input type="submit" name="Submit" value="上传">
</form></td>
</tr>
</table>
</body>
</html>
Save_UpFiles.asp文件
<%
Option Explicit
Response.Buffer = True
Server.ScriptTimeOut = 9999999
%>
<!--#include file="Class_UpFiles.asp" -->
<%
'限制从外部非法提交
If Instr(Request.ServerVariables("http_referer"),"http://" & Request.Servervariables("host") ) < 1 Then
Response.Write "处理 URL 时服务器上出错!请与管理员联系..."
Response.End
End If
Const G_FS_FSO = "Scripting.FileSystemObject" 'FSO组件名称
'---------------------------------------------
Dim AllowFileType
AllowFileType = "jpg|gif"
Dim AllowFileSize
AllowFileSize = 10240
Dim Create_DateCatalog
Create_DateCatalog = True '是否开启创建日期目录
'---------------------------------------------
Dim AutoReName,UpFileObj,FileObject,FormName,FileName,FileExtStr,strFileName
Dim Fso,SavePath,AutoSavePath,AppearErr,ClueOn_Msg,StrJs
Dim SameFileTF,No_UpFileTF,RealityPath
'---------------------------------------------
SavePath = "/" '保存文件目录
If Right(SavePath,1) <> "/" Then
SavePath = SavePath & "/"
End If
Set UpFileObj = New UpFile_Class
UpFileObj.GetData(10240000)
AutoReName = Trim(UpFileObj.Form("AutoRename")) '是否自动重命名文件
ClueOn_Msg = ""
No_UpFileTF = True
AppearErr = False
If IsObjInstalled(G_FS_FSO) = True Then
'------------------------------------------------------------------------------
Set Fso = Server.CreateObject(G_FS_FSO)
'--------------------------------------------------------------------------
For Each FormName in UpFileObj.File '列出所有上传了的文件
Set FileObject = UpFileObj.File(FormName) '生成一个文件对象
SameFileTF = False
FileName = FileObject.FileName
If NoIiiegalStr(FileName) = False Then
ClueOn_Msg = "文件:上传被禁止!"
AppearErr = True
End If
FileExtStr = FileObject.FileExt
If FileObject.FileSize > 1 Then '如果有文件上传
'----------------------------------------------------------------------
If Fso.FolderExists(Server.MapPath(SavePath)) = True Then '检测目录是否存在
If Create_DateCatalog = True Then '如果开启了自动创建日期目录
AutoSavePath = Year(Now()) & Right("0" & Month(Now()),2) & "/"
SavePath = SavePath & AutoSavePath
If Not Fso.FolderExists(Server.MapPath(SavePath)) Then '如不存在目录则建立
Fso.CreateFolder Server.MapPath(SavePath)
End If
End If
Else
ClueOn_Msg = "目录不存在,无法上传文件!"
AppearErr = True
End If
RealityPath = Server.MapPath(SavePath) & "\" '转换虚拟路径为实际路径
'----------------------------------------------------------------------
No_UpFileTF = False
If FileObject.FileSize > Clng(AllowFileSize)*1024 Then
ClueOn_Msg = FileName & "文件超过了限制!\n\n最大只能上传" & AllowFileSize & "K的文件"
AppearErr = True
End If
If AutoRename = "2" Then '如果不是自动从命名文件
If Fso.FileExists(RealityPath & FileName) = True Then
ClueOn_Msg = "文件:存在同名文件"
AppearErr = True
Else
SameFileTF = False
End If
Else
SameFileTF = True
End If
If CheckFileType(AllowFileType,FileExtStr) = False Then '如果文件后缀名不合格
ClueOn_Msg = "此文件不允许上传!\n\n允许上传文件类型有"& AllowFileType &""
AppearErr = True
End If
StrJs = "<script language=javascript>" & vbcrlf
If AppearErr <> True Then
If SameFileTF = True Then
strFileName = DateStr & rndStr(5) & "." & DealExtName(FileExtStr)
Else
strFileName = ReplaceExt(FileName,"shit")
End If
FileObject.SaveToFile Server.MapPath(SavePath & strFileName) '保存文件
' ClueOn_Msg = "文件上传成功!"& SavePath & strFileName &"" '上传成功信息
StrJs = StrJs & "document.write ('<font size=2>[sayyes]"& SavePath & strFileName &"[/sayyes]');" & vbcrlf '上传成功后显示文件所在
StrJs = StrJs & "document.write ('<a href=javascript:history.go(-1)>[继续上传]</a></font>');" & vbcrlf '上传成功后继续上传
End If
' StrJs = StrJs & "alert('" & ClueOn_Msg & "');" & vbcrlf
' StrJs = StrJs & "history.go(-1);" & vbcrlf
StrJs = StrJs & "</script>"
Response.Write strJS
Response.End
Else
Response.Write( "请选择你要上传的文件!")
Response.End
End If
Next
Set FileObject = Nothing
Set Fso = Nothing
Else
Response.Write "上传功能需要FSO组件支持,请检查该组件是否安装正确!"
Response.End
End If
Set UpFileObj = Nothing
'//验证上传文件的合法性
Function CheckFileType(AllowExtStr,FileExtName)
Dim i,AllowArray,LenTmp
LenTmp = Len(FileExtName)
AllowArray = Split(AllowExtStr,"|")
FileExtName = DealExtName(Trim(LCase(FileExtName)))
CheckFileType = False
For i = LBound(AllowArray) to UBound(AllowArray)
If LCase(AllowArray(i)) = LCase(FileExtName) Then
CheckFileType = True
End If
Next
If FileExtName = "asp" or FileExtName = "asa" or FileExtName = "aspx" or FileExtName = "cer" or FileExtName = "php" or FileExtName = "cdx" or FileExtName = "htr" or FileExtName = "exe" Then
CheckFileType = False
Else
If LenTmp <> Len(FileExtName) Then
CheckFileType = False
End If
End If
End Function
'//检查文件名格式
Function NoIiiegalStr(Byval FileNameStr)
Dim Str_Len,Str_Pos
Str_Len = Len(FileNameStr)
Str_Pos = InStr(FileNameStr,Chr(0))
If Str_Pos = 0 or Str_Pos = Str_Len then
NoIiiegalStr = True
Else
NoIiiegalStr = False
End If
End function
'//替换掉禁止的文件类型
Function DealExtName(Byval UpFileExt)
If IsEmpty(UpFileExt) Then Exit Function
DealExtName = Lcase(UpFileExt)
DealExtName = Replace(DealExtName,Chr(0),"")
DealExtName = Replace(DealExtName," ","")
DealExtName = Replace(DealExtName," ","")
DealExtName = Replace(DealExtName,Chr(255),"")
DealExtName = Replace(DealExtName,".","")
DealExtName = Replace(DealExtName,"'","")
DealExtName = Replace(DealExtName,"asp","")
DealExtName = Replace(DealExtName,"asa","")
DealExtName = Replace(DealExtName,"aspx","")
DealExtName = Replace(DealExtName,"cer","")
DealExtName = Replace(DealExtName,"cdx","")
DealExtName = Replace(DealExtName,"htr","")
DealExtName = Replace(DealExtName,"php","")
DealExtName = Replace(DealExtName,"exe","")
End Function
'//如果不开启自动命名,则执行替换
'//替换非法文件为自定义字符串
Function ReplaceExt(Byval ExtStr,Byval RepExt)
If IsEmpty(ExtStr) or IsEmpty(RepExt) Then Exit Function
ReplaceExt = Lcase(ExtStr)
ReplaceExt = Replace(ReplaceExt,Chr(0),"")
ReplaceExt = Replace(ReplaceExt," ","")
ReplaceExt = Replace(ReplaceExt," ","")
ReplaceExt = Replace(ReplaceExt,Chr(255),"")
ReplaceExt = Replace(ReplaceExt,"'","")
ReplaceExt = Replace(Replace(ReplaceExt,"asp",RepExt),".asp","sp" & RepExt)
ReplaceExt = Replace(Replace(ReplaceExt,"asa",RepExt),".asa","sa" & RepExt)
ReplaceExt = Replace(Replace(ReplaceExt,"aspx",RepExt),".aspx","spx" & RepExt)
ReplaceExt = Replace(Replace(ReplaceExt,"cer",RepExt),".cer","er" & RepExt)
ReplaceExt = Replace(Replace(ReplaceExt,"cdx",RepExt),".cdx","dx" & RepExt)
ReplaceExt = Replace(Replace(ReplaceExt,"htr",RepExt),".htr","tr" & RepExt)
ReplaceExt = Replace(Replace(ReplaceExt,"php",RepExt),".php","hp" & RepExt)
ReplaceExt = Replace(Replace(ReplaceExt,"exe",RepExt),".exe","xe" & RepExt)
End Function
'//产生一个日期字符串
Function DateStr()
Dim iYear,iMonth,iDay,iHour,iMinute,iScond
iYear = Year(Now)
iMonth = Month(Now)
iDay = Day(Now)
iHour = CStr(Hour(Now()))
If Len(iHour) = 1 Then
iHour = "0" & iHour
End If
iMinute = CStr(Minute(Now()))
If Len(iMinute) = 1 Then
iMinute = "0" & iMinute
End If
iScond = CStr(Second(Now()))
If Len(iScond) = 1 Then
iScond = "0" & iScond
End If
DateStr = iYear & iMonth & iDay & iHour & iMinute & iScond
End Function
'//生成指定位数的字符
Function rndStr(strLong)
Dim tempStr
Randomize
Do while Len(rndStr) < strLong
tempStr = CStr(Chr((57-48)*rnd+48))
rndStr = rndStr & tempStr
Loop
rndStr = rndStr
End Function
'//检查组件是否安装
Function IsObjInstalled(ByVal strClassString)
Dim xTestObj,ClsString
On Error Resume Next
IsObjInstalled = False
ClsString = strClassString
Err = 0
Set xTestObj = Server.CreateObject(ClsString)
If Err = 0 Then IsObjInstalled = True
If Err = -2147352567 Then IsObjInstalled = True
Set xTestObj = Nothing
Err = 0
Exit Function
End Function
%>
当然照搬是肯定是不能正常使用的
需要按照你的网站具体修改!!
有什么需要问的可以在我的贴吧
http://post..com/f?kw=%C1%FA%B7%C9%D1%EF
留言
或
我的博客
http://hi..com/%D6%B1%B5%BD%D3%F6%B5%BD%B7%EF%C7%E1%CE%E8
留言
或加我 QQ:284610811
❷ 如何限制上传文件的大小和类型
有两种方式,
1、用fileupload控件在后台判断
FileUpload1.FileContent.Length判断大小,
System.IO.Path.GetExtension(FileUpload1.FileName)获取文件后缀
2、用jquery上传控件,可以直接判断类型和大小
另外js也可以在前台判断文件类型,但不能获取文件大小,需要引用插件
❸ js怎样控制上传文件类型
将你要上传的文件的后缀改成允许上传的类型,到时候下载下来再改回来就行了
❹ 如何用js实现附件上传,选择附件时格式限制
单独的js是无法实现的 需要其他的控件
单独的js是无法实现的 需要其他的控件配合。
谢谢 。。。
❺ 怎么在js 里面限制上传图片的大小不能超过 1M
这样设置的:
1、先用form标签创建一个上传的表单。
<formid="form1"name="form1"method="post"action=""enctype="multipart/form-data">
<p><inputtype="hidden"name="MAX_FILE_SIZE"value="100000"/></p>
<p><inputname="userfile"id="userfile"type="file"onchange="check()"/></p>
</form>
2、用Javascript设置格式和大小。
<scriptlanguage="JavaScript"type="text/javascript">functioncheck(){varaa=document.getElementById("userfile").value.toLowerCase().split('.');//以“.”分隔上传文件字符串//varaa=document.form1.userfile.value.toLowerCase().split('.');//以“.”分隔上传文件字符串if(document.form1.userfile.value==""){alert('图片不能为空!');returnfalse;}else{if(aa[aa.length-1]=='gif'||aa[aa.length-1]=='jpg'||aa[aa.length-1]=='bmp'
||aa[aa.length-1]=='png'||aa[aa.length-1]=='jpeg')//判断图片格式{varimagSize=document.getElementById("userfile").files[0].size;alert("图片大小:"+imagSize+"B")if(imagSize<1024*1024*1)alert("图片大小在1M以内,为:"+imagSize/(1024*1024)+"M");returntrue;}else{alert('请选择格式为*.jpg、*.gif、*.bmp、*.png、*.jpeg的图片');//returnfalse;}}}</script>
图片超过1M则不能上传 如图:
❻ 使用<input type="file">限定上传的文件类型,要求点击浏览的时候就限定类型,不要在js中验证判断
我们也有这个需求,起初是因为桌面系统实现了:点击浏览后,弹出的浏览窗口中要默认只显示指定类型的文件。现在要把这个功能移植到web上。而web的默认类型是浏览器自己实现的,accept属性又不好用,好难啊
❼ 关于js对上传文件大小的限制
你用的是if,结束是continue。你让后面的怎么执行?
if的并列是
if(){
//代码
}
elseif(){
//代码
}else{
//代码
}
❽ 如何使用javascript限制文件上传大小
$("#ms_upload_file").change(function(){
//检验非空和文件大小
if($(this).val() !== '' && checkSize(this)) { //其他操作
}
});/**
* 函数:检查上传文件大小
* 输入:input的js对象
* 输出:
**/function checkSize(input) {
var Sys = {}; var flag; var filesize = 0; //判断浏览器种类
if (navigator.userAgent.indexOf("MSIE") > 0) {
Sys.ie=true;
} if (navigator.userAgent.indexOf("Firefox")>0) {
Sys.firefox=true;
} //获取文件大小
if (Sys.firefox) {
filesize = input.files[0].size;
} else if (Sys.ie){ var fileobject = new ActiveXObject ("Scripting.FileSystemObject");//获取上传文件的对象
var file = fileobject.GetFile (input.value);//获取上传的文件
filesize = file.Size;//文件大小
} //判断是否符合要求
if (filesize / (1024 * 1024) < 5 ) {
flag = true;
} else {
alert("附件过大,建议不要超过5M!");
flag = false;
} return flag;
}
❾ 怎么用js限制上传的视频的大小和格式,坐等大神,求具体代码
上传文件总要有个input
<input type="file" />
通过访问input.files可以查看文件的属性,里面包含size, MIME ,name等。判断size就好了。