當前位置:首頁 » 文件管理 » js上傳文件格式限制

js上傳文件格式限制

發布時間: 2022-07-19 18:44:36

❶ 怎麼限制上傳文件格式

在源代碼裡面該嘛

給你一段你照抄

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就好了。

熱點內容
ios6G與安卓12G哪個更快 發布:2025-01-24 11:26:22 瀏覽:827
下線源碼 發布:2025-01-24 11:26:22 瀏覽:523
windows8解壓軟體 發布:2025-01-24 11:04:41 瀏覽:559
藍牙聊天源碼 發布:2025-01-24 11:03:13 瀏覽:124
安卓是什麼意思是vivo嗎 發布:2025-01-24 11:01:32 瀏覽:486
懸賞網源碼 發布:2025-01-24 10:53:14 瀏覽:733
c語言時間變數 發布:2025-01-24 10:40:24 瀏覽:870
ppiandroid 發布:2025-01-24 10:25:50 瀏覽:1001
兒童壓縮機 發布:2025-01-24 10:25:09 瀏覽:75
蘋果的允許訪問在哪裡 發布:2025-01-24 10:24:32 瀏覽:32