當前位置:首頁 » 文件傳輸 » asp無組件上傳
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

asp無組件上傳

發布時間: 2022-01-14 17:21:56

1. ASP怎樣無組件上傳文件到指定文件夾,並且將路徑寫入資料庫如題 謝謝了

<head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> <title>示例結果</title> <link href="../style.css" rel="stylesheet" type="text/css"> </head> <body style="font-size:12px"> <% Dim Upload,successful '=============================================================================== set Upload=new AnUpload '創建類實例 Upload.SingleSize=1024*1024 '設置單個文件最大上傳限制,按位元組計;默認為不限制 Upload.MaxSize=4096*1024 '設置最大上傳限制,按位元組計;默認為不限制 Upload.Exe="rar|jpg|bmp|gif|doc|xls|ppt|txt" '設置合法擴展名,以|分割,忽略大小寫 Upload.GetData() '獲取並保存數據,必須調用本方法 '=============================================================================== if Upload.Err>0 then '判斷錯誤號,如果myupload.Err<=0表示正常 response.write Upload.Description '如果出現錯誤,獲取錯誤描述 else response.Write "表單text1的值為: " & Upload.Forms("text1") & "<br />" if Upload.forms("file1")<>"" then '這里判斷你file1是否選擇了文件 path=server.mappath("files") '文件保存路徑(這里是files文件夾) set tempCls=Upload.files("file1") successful=tempCls.SaveToFile(path,0) '以時間+隨機數字為文件名保存 'successful=tempCls.SaveToFile(path,1) '如果想以原文件名保存,請使用本句 if successful then response.write tempCls.FileName & "上傳完畢,大小為" & Upload.getsize(tempCls.Size) & ";<br />本地路徑" & Upload.forms("file1") & "!<br />" Response.Redirect "InsertDatabase.asp?FileName="&Upload.Forms("text1")&"&FilePath="&tempCls.FileName else response.write "上傳失敗" end if set tempCls=nothing end if end if set Upload=nothing '銷毀類實例 %> </body> </html>
採納哦

2. ASP怎麼能實現無組件上傳啊

功能簡介
' 將上傳的文件數據保存到資料庫中,可以處理表單中的多個上傳文件的情況
' 適用於各種資料庫,使用ADO的方法連接資料庫
' 本示例中使用的是ACCESS資料庫:zj.mdb
' 表:tb_img(id int(自增列),path text(255) 保存上傳文件的目錄
' ,fname text(250) 保存上傳的文件名,type test(250) 保存上傳文件的類型
' ,img ole對象 保存上傳的文件內容
'
'
'==================================================================

'==================================================================
'
' 上傳文件的HTML頁: zj_up.htm
'
'==================================================================

代碼:

<html>
<head>
<title>文件上傳保存到資料庫中</title>
</head>
<body>
<form name="form1" enctype="multipart/form-data" method="post" action="zj_up.asp">
<p>
<input type="file" name="file">
<input type="submit" name="Submit" value="上傳">
</p>
</form>
</body>
</html>

'==================================================================
'
' 上傳文件保存到資料庫的ASP頁: zj_up.asp
'
'==================================================================

代碼:

<%
Response.Expires=0
Function f_Bin2Str(ByVal sBin)
Dim iI, iLen, iChr, iRe
iRe = ""
If Not IsNull(sBin) Then
iLen = LenB(sBin)
For iI = 1 To iLen
iChr = MidB(sBin, iI, 1)
If AscB(iChr) > 127 Then
iRe = iRe & Chr(AscW(MidB(sBin, iI + 1, 1) & iChr))
iI = iI + 1
Else
iRe = iRe & Chr(AscB(iChr))
End If
Next
End If
f_Bin2Str = iRe
End Function
iConcStr = "Provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info=False" & _
";Data Source=" & server.mappath("zj.mdb")
iSql="tb_img"
set iRe=Server.CreateObject("ADODB.Recordset")
iRe.Open iSql,iConcStr,1,3
iLen=Request.TotalBytes
sBin=Request.BinaryRead(iLen)
iCrlf1 = ChrB(13) & ChrB(10)
iCrlf2 = iCrlf1 & iCrlf1
iLen = InStrB(1, sBin, iCrlf1) - 1
iSpc = LeftB(sBin, iLen)
sBin = MidB(sBin, iLen + 34)
iPos1 = InStrB(sBin, iCrlf2) - 1
While iPos1 > 0
iStr = f_Bin2Str(LeftB(sBin, iPos1))
iPos1 = iPos1 + 5
iPos2 = InStrB(iPos1, sBin, iSpc)

iPos3 = InStr(iStr, "; filename=""") + 12
If iPos3 > 12 Then
iStr = Mid(iStr, iPos3)
iPos3 = InStr(iStr, Chr(13) & Chr(10) & "Content-Type: ") - 2
iFn = Left(iStr, iPos3)
If iFn <> "" Then
iRe.AddNew
ire("path")=left(iFn,instrrev(iFn,""))
iRe("fname") = mid(iFn,instrrev(iFn,"")+1)
iRe("type") = Mid(iStr, iPos3 + 18)
iRe("img").AppendChunk MidB(sBin, iPos1, iPos2 - iPos1)
iRe.Update
End If
End If

sBin = MidB(sBin, iPos2 + iLen + 34)
iPos1 = InStrB(sBin, iCrlf2) - 1
Wend
iRe.close
set iRe=Nothing
%>

'==================================================================
'
' 下載數據的ASP頁: zj_down.asp
'
'==================================================================

代碼:

<%
Response.Buffer=true
Response.Clear

iConcStr = "Provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info=False" & _
";Data Source=" & server.mappath("zj.mdb")
set iRe=server.createobject("adodb.recordset")
iSql="tb_img"
iRe.open iSql,iconcstr,1,1
Response.ContentType=ire("type")
Response.BinaryWrite iRe("img")

iRe.close
set iRe=Nothing
%>

3. asp代碼怎樣實現無組件上傳

一、無組件上傳的原理
我還是一點一點用一個實例來說明的吧,客戶端HTML如下。要瀏覽上傳附件,我們通過<input type="file">元素,但是一定要注意必須設置form的enctype屬性為"multipart/form-data":

<form method="post" action="upload.asp" enctype="multipart/form-data">
<label>
<input type="file" name="file1" />
</label>

<input type="text" name="filename" value="default filename"/>

<input type="submit" value="Submit"/>
<input type="reset" value="Reset"/>
</form>

在後台asp程序中,以前獲取表單提交的ASCII 數據,非常的容易。但是如果需要獲取上傳的文件,就必須使用Request對象的BinaryRead方法來讀取。BinaryRead方法是對當前輸入流進行指定位元組數的二進制讀取,有點需要注意的是,一旦使用BinaryRead 方法後,再也不能使用Request.Form 或 Request.QueryString 集合了。結合Request對象的TotalBytes屬性,可以將所有表單提交的數據全部變成二進制,不過這些數據都是經過編碼的。首先讓我們來看看這些數據是如何編碼的,有無什麼規律可循,編段代碼,在代碼中我們將BinaryRead讀取的二進制轉化為文本,輸出出來,在後台的upload.asp中(注意該示例不要上傳大文件,否則可能會造成瀏覽器死掉):
<%
Dim biData, PostData
Size = Request.TotalBytes
biData = Request.BinaryRead(Size)
PostData = BinaryToString(biData,Size)
Response.Write "<pre>" & PostData & "</pre>" '使用pre,原樣輸出格式
' 藉助RecordSet將二進制流轉化成文本
Function BinaryToString(biData,Size)
Const adLongVarChar = 201
Set RS = CreateObject("ADODB.Recordset")
RS.Fields.Append "mBinary", adLongVarChar, Size
RS.Open
RS.AddNew
RS("mBinary").AppendChunk(biData)
RS.Update
BinaryToString = RS("mBinary").Value
RS.Close
End Function
%>

4. 求一個asp無組件上傳代碼,有要求,滿意著追加50分

<!--#include file="upload.asp"-->
<%
const upload_type=0 '上傳方法:0=無懼無組件上傳類,1=FSO上傳 2=lyfupload,3=aspupload,4=chinaaspupload

dim upload,file,formName,SavePath,filename,fileExt
dim upNum
dim EnableUpload
dim Forumupload
dim ranNum
dim uploadfiletype
dim msg,founderr
msg=""
founderr=false
EnableUpload=false
SavePath = "../DateBasc/" '存放上傳文件的目錄
if right(SavePath,1)<>"/" then SavePath=SavePath&"/" '在目錄後加(/)
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head>
<body leftmargin="2" topmargin="0" marginwidth="0" marginheight="0">
<%
if EnableUploadFile="No" then
response.write "系統未開放文件上傳功能"
else
if session("admin")="" and session("UserName")="" then
response.Write("請登錄後再使用本功能!")
else
select case upload_type
case 0
call upload_0() '使用化境無組件上傳類
case else
'response.write "本系統未開放插件功能"
'response.end
end select
end if
end if
%>
</body>
</html>
<%
sub upload_0() '使用化境無組件上傳類
set upload=new upload_file '建立上傳對象
for each formName in upload.file '列出所有上傳了的文件
set file=upload.file(formName) '生成一個文件對象
if file.filesize<100 then
msg="請先選擇你要上傳的文件!"
founderr=true
end if
if file.filesize>(MaxFileSize*1024) then
msg="文件大小超過了限制,最大隻能上傳" & CStr(MaxFileSize) & "K的文件!"
founderr=true
end if

fileExt=lcase(file.FileExt)
Forumupload=split(UpFileType,"|")
for i=0 to ubound(Forumupload)
if fileEXT=trim(Forumupload(i)) then
EnableUpload=true
exit for
end if
next
if fileEXT="asp" or fileEXT="asa" or fileEXT="aspx" then
EnableUpload=false
end if
if EnableUpload=false then
msg="這種文件類型不允許上傳!\n\n只允許上傳這幾種文件類型:" & UpFileType
founderr=true
end if
strJS="<SCRIPT language=javascript>" & vbcrlf
if founderr<>true then
randomize
ranNum=int(900*rnd)+100
FileName=SavePath&upload.form("name")&".xls"

file.SaveToFile Server.mappath(FileName) '保存文件

msg="文件上傳成功!"

end if
strJS=strJS & "alert('" & msg & "');" & vbcrlf
if session("filename")<>"" then
strJS=strJS & "document.location='add_danan.asp','right';" & vbcrlf
else
strJS=strJS & "history.go(-1);" & vbcrlf
end if
strJS=strJS & "</script>"
response.write strJS
next
set upload=nothing
end sub
%>

下面是upload.asp文件
<%
'----------------------------------------------------------------------
'轉發時請保留此聲明信息,這段聲明不並會影響你的速度!
'******************* 無組件上傳類 ********************************
'修改者:梁無懼
'電子郵件:[email protected]
'網站:http://www.25cn.com
'原作者:稻香老農
'原作者網站:http://www.5xsoft.com
'聲明:此上傳類是在化境編程界發布的無組件上傳類的基礎上修改的.
'在與化境編程界無組件上傳類相比,速度快了將近50倍,當上傳4M大小的文件時
'伺服器只需要10秒就可以處理完,是目前最快的無組件上傳程序,當前版本為0.96
'源代碼公開,免費使用,對於商業用途,請與作者聯系
'文件屬性:例如上傳文件為c:\myfile\doc.txt
'FileName 文件名 字元串 "doc.txt"
'FileSize 文件大小 數值 1210
'FileType 文件類型 字元串 "text/plain"
'FileExt 文件擴展名 字元串 "txt"
'FilePath 文件原路徑 字元串 "c:\myfile"
'使用時注意事項:
'由於Scripting.Dictionary區分大小寫,所以在網頁及ASP頁的項目名都要相同的大小
'寫,如果人習慣用大寫或小寫,為了防止出錯的話,可以把
'sFormName = Mid (sinfo,iFindStart,iFindEnd-iFindStart)
'改為
'(小寫者)sFormName = LCase(Mid (sinfo,iFindStart,iFindEnd-iFindStart))
'(大寫者)sFormName = UCase(Mid (sinfo,iFindStart,iFindEnd-iFindStart))
'**********************************************************************
'----------------------------------------------------------------------
dim oUpFileStream

Class upload_file

dim Form,File,Version

Private Sub Class_Initialize
'定義變數
dim RequestBinDate,sStart,bCrLf,sInfo,iInfoStart,iInfoEnd,tStream,iStart,oFileInfo
dim iFileSize,sFilePath,sFileType,sFormvalue,sFileName
dim iFindStart,iFindEnd
dim iFormStart,iFormEnd,sFormName
'代碼開始
Version="無組件上傳類 Version 0.96"
set Form = Server.CreateObject("Scripting.Dictionary")
set File = Server.CreateObject("Scripting.Dictionary")
if Request.TotalBytes < 1 then Exit Sub
set tStream = Server.CreateObject("adodb.stream")
set oUpFileStream = Server.CreateObject("adodb.stream")
oUpFileStream.Type = 1
oUpFileStream.Mode = 3
oUpFileStream.Open
oUpFileStream.Write Request.BinaryRead(Request.TotalBytes)
oUpFileStream.Position=0
RequestBinDate = oUpFileStream.Read
iFormEnd = oUpFileStream.Size
bCrLf = chrB(13) & chrB(10)
'取得每個項目之間的分隔符
sStart = MidB(RequestBinDate,1, InStrB(1,RequestBinDate,bCrLf)-1)
iStart = LenB (sStart)
iFormStart = iStart+2
'分解項目
Do
iInfoEnd = InStrB(iFormStart,RequestBinDate,bCrLf & bCrLf)+3
tStream.Type = 1
tStream.Mode = 3
tStream.Open
oUpFileStream.Position = iFormStart
oUpFileStream.CopyTo tStream,iInfoEnd-iFormStart
tStream.Position = 0
tStream.Type = 2
tStream.Charset ="gb2312"
sInfo = tStream.ReadText
'取得表單項目名稱
iFormStart = InStrB(iInfoEnd,RequestBinDate,sStart)-1
iFindStart = InStr(22,sInfo,"name=""",1)+6
iFindEnd = InStr(iFindStart,sInfo,"""",1)
sFormName = Mid (sinfo,iFindStart,iFindEnd-iFindStart)
'如果是文件
if InStr (45,sInfo,"filename=""",1) > 0 then
set oFileInfo= new FileInfo
'取得文件屬性
iFindStart = InStr(iFindEnd,sInfo,"filename=""",1)+10
iFindEnd = InStr(iFindStart,sInfo,"""",1)
sFileName = Mid (sinfo,iFindStart,iFindEnd-iFindStart)
oFileInfo.FileName = GetFileName(sFileName)
oFileInfo.FilePath = GetFilePath(sFileName)
oFileInfo.FileExt = GetFileExt(sFileName)
iFindStart = InStr(iFindEnd,sInfo,"Content-Type: ",1)+14
iFindEnd = InStr(iFindStart,sInfo,vbCr)
oFileInfo.FileType = Mid (sinfo,iFindStart,iFindEnd-iFindStart)
oFileInfo.FileStart = iInfoEnd
oFileInfo.FileSize = iFormStart -iInfoEnd -2
oFileInfo.FormName = sFormName
file.add sFormName,oFileInfo
else
'如果是表單項目
tStream.Close
tStream.Type = 1
tStream.Mode = 3
tStream.Open
oUpFileStream.Position = iInfoEnd
oUpFileStream.CopyTo tStream,iFormStart-iInfoEnd-2
tStream.Position = 0
tStream.Type = 2
tStream.Charset = "gb2312"
sFormvalue = tStream.ReadText
form.Add sFormName,sFormvalue
end if
tStream.Close
iFormStart = iFormStart+iStart+2
'如果到文件尾了就退出
loop until (iFormStart+2) = iFormEnd
RequestBinDate=""
set tStream = nothing
End Sub

Private Sub Class_Terminate
'清除變數及對像
if not Request.TotalBytes<1 then
oUpFileStream.Close
set oUpFileStream =nothing
end if
Form.RemoveAll
File.RemoveAll
set Form=nothing
set File=nothing
End Sub

'取得文件路徑
Private function GetFilePath(FullPath)
If FullPath <> "" Then
GetFilePath = left(FullPath,InStrRev(FullPath, "\"))
Else
GetFilePath = ""
End If
End function

'取得文件名
Private function GetFileName(FullPath)
If FullPath <> "" Then
GetFileName = mid(FullPath,InStrRev(FullPath, "\")+1)
Else
GetFileName = ""
End If
End function

'取得擴展名
Private function GetFileExt(FullPath)
If FullPath <> "" Then
GetFileExt = mid(FullPath,InStrRev(FullPath, ".")+1)
Else
GetFileExt = ""
End If
End function

End Class

'文件屬性類
Class FileInfo
dim FormName,FileName,FilePath,FileSize,FileType,FileStart,FileExt
Private Sub Class_Initialize
FileName = ""
FilePath = ""
FileSize = 0
FileStart= 0
FormName = ""
FileType = ""
FileExt = ""
End Sub

'保存文件方法
Public function SaveToFile(FullPath)
dim oFileStream,ErrorChar,i
SaveToFile=1
if trim(fullpath)="" or right(fullpath,1)="/" then exit function
set oFileStream=CreateObject("Adodb.Stream")
oFileStream.Type=1
oFileStream.Mode=3
oFileStream.Open
oUpFileStream.position=FileStart
oUpFileStream.to oFileStream,FileSize
oFileStream.SaveToFile FullPath,2
oFileStream.Close
set oFileStream=nothing
SaveToFile=0
end function
End Class
%>

5. ASP無組件與有組件上傳問題

這個估計要到IIS里修改哈,因為IIS默認上傳有限制的。

6. 如何在asp中使用無組件上傳文件!

2個文件
第一個文件:
<html>
<body>
<center>
<form name="mainForm" enctype="multipart/form-data" action="process.asp" method=post>
<input type=file name=mefile><br>
<input type=submit name=okvalue="OK">
</form>
</center>
</body>
</html>

第二個:
<%
sub pic()
if Request.QueryString("submit")="pic" then
uploadpath="image/face/"
uploadsize="2048"
uploadtype="jpg/gif/png/bmp"
Set Uprequest=new UpLoadClass
Uprequest.SavePath=uploadpath
Uprequest.MaxSize=uploadsize*1024
Uprequest.FileType=uploadtype
AutoSave=true
Uprequest.open
if Uprequest.form("file_Err")<>0 then
select case Uprequest.form("file_Err")
case 1:str="<div style=""padding-top:5px;padding-bottom:5px;""> <font color=blue>上傳不成功!文件超過"&uploadsize&"k [<a href='javascript:history.go(-1)'>重新上傳</a>]</font></div>"
case 2:str="<div style=""padding-top:5px;padding-bottom:5px;""> <font color=blue>上傳不成功!文件格式不對 [<a href='javascript:history.go(-1)']>重新上傳</a>]</font></div>"
case 3:str="<div style=""padding-top:5px;padding-bottom:5px;""> <font color=blue>上傳不成功!文件太大且格式不對 [<a href='javascript:history.go(-1)'>重新上傳</a>]</font></div>"
end select
response.write str
else
response.write "<script language=""javascript"">parent.form2.pic.value='"&Uprequest.SavePath&Uprequest.Form("file")&"';"
response.write "</script>"
response.write "</script>"
size=Uprequest.Form("file_size")
showsize=size & " Byte"
if size>1024 then
size=(size\1024)
showsize=size & " KB"
end if
if size>1024 then
size=(size/1024)
showsize=formatnumber(size,2) & " MB"
end if
response.write "<script language=""javascript"">parent.form2.url.value='"&showsize&"';"
response.write "</script>"
response.write "<div style=""padding-top:5px;padding-bottom:5px;""> <font color=red>文件上傳成功</font> [<a href='javascript:history.go(-1)'>重新上傳</a>]</div>"
end if
Set Uprequest=nothing
end if
response.write "<form name=form action=?action=pic&submit=pic method=post enctype=multipart/form-data>"
response.write "<input type=file name=file class='tx' size='20'> "
response.write "<input type=submit name=submit value=上傳 class=""tx1"">"
response.write "</form>"
end sub
%>

7. ASP無組件上傳代碼,只求簡單!

dim upload,file
set upload=new UpFile_Class ''建立上傳對象
upload.GetDate (2048*1024) '取得上傳數據,限大小2M

if upload.err > 0 then
select case upload.err
case 1
Response.Write "請先選擇你要上傳的文件 [ <a href=# onclick=history.go(-1)>重新上傳</a> ]"
case 2
Response.Write "文件大小超過了限制 2048K [ <a href=# onclick=history.go(-1)>重新上傳</a> ]"
end select

else

formPath="UploadFile/" '上傳目錄
for each formName in upload.file ''列出所有上傳了的文件
set file=upload.file(formName) ''生成一個文件對象
fileExt=lcase(file.FileExt) '取文件後綴名

'付值變數
randomize
ranNum=int(90000*rnd)+10000 '生成隨機數
F_Type=CheckFiletype(fileEXT)
file_name=year(now)&month(now)&day(now)&hour(now)&minute(now)&second(now)&ranNum
filename=file_name&"."&fileExt
rename=filename&"|"
filename=formPath&filename '以上根據時間及隨機數
生成新的文件名
Filesize=file.FileSize '取文件大小

'記錄文件
if Filesize>0 then '如果 FileSize > 0 說明有文件數據
file.SaveToFile Server.mappath(FileName) ''執行上傳文件
end if
set file=nothing
next
end if
set upload=nothing

8. 用ASP實現無組件上傳/下載文件

調試程序,建議你關閉瀏覽器的「顯示友好的http錯誤」重新啟動瀏覽器以查看錯誤的詳細情況你就會看到例如:

Active Directory 錯誤 '80070057'

一個或數個參數無效

/cs.asp,行 3

的錯誤內容,以便修改更正!
(如果關閉「顯示友好的http錯誤」後還是提示500錯誤,你可能需要把iis伺服器中該站點的500自定義錯誤設置為默認值)

9. asp無組件上傳

6行代碼實現無組件上傳

目前有很多無組件上傳類,我大概看了一下,大多寫的相當復雜,有的居然還只能傳文本最關鍵的是沒有10行代碼以下的 :)我花了一個晚上時間研究了一下ADODB.Stream,並且用了6行代碼實現了無組件上傳:

strFileName = Request.QueryString("file1")
Set objStream = Server.CreateObject("ADODB.Stream")
objStream.Type = 1 ' adTypeBinary
objStream.Open
objStream.LoadFromFile strFileName
objStream.SaveToFile Server."123_onweb.gif",2

使用方法:
把上面的代碼寫成upload.asp
在瀏覽器裡面輸入:
http://XXX/upload.asp?file1=c:\上傳文件\123.gif
XXX為你的主機地址
執行完後你會看到你的目錄下面多了一個123_onweb.gif
他就是你要文件拉!!!!

根據原理我們可以擴展以下代碼:
upload.asp文件
<%
Function GetFileName(ByVal strFile)
If strFile <> "" Then
GetFileName = mid(strFile,InStrRev(strFile, "\")+1)
Else
GetFileName = ""
End If
End function

strFileName = Request.Form("file1")
Set objStream = Server.CreateObject("ADODB.Stream")
objStream.Type = 1 ' adTypeBinary
objStream.Open
objStream.LoadFromFile strFileName
objStream.SaveToFile Server.MapPath(GetFileName(strFileName)),2
objStream.Close
%>

upload.htm文件
<form name="FORM" action="upload.asp" method="post">
<input type="submit" name="submit" value="OK">
<input type="file" name="file1" style="width:400" value="">
</form>

10. asp無組件上傳如何實現上傳pdf文件

把源文件發過來看看!