asp上傳下載代碼
1. 求!!ASP上傳下載源碼,那位大俠提供下,小弟感激不盡。。。
風雲ASP(組件&無組件)上傳 [2006-11-30]
最新的組件發布地址是:http://www.zope.org/Members/Rimifon/FyUpload.sct,需要最新版本,請留意該文件的最後更新時間
有組件上傳:
第一次使用需要右鍵「FyUpload.sct」文件 - 注冊
創建對象:Set Upload = Server.CreateObject("Rimifon.Upload")
無組件上傳:
創建對象:Set Upload = GetObject("script:組件地址#Upload")
組件地址為硬碟絕對路徑,也可以為http完整路徑(針對IIS 5.0)
簡單測試代碼:
/-*- Upload.html -*-/
<form method=post action=Upload.asp enctype=multipart/form-data>
<input name=MyFile type=file>
<input type=submit></form>
/-*- Upload.asp -*-/
<%Set Upload = Server.CreateObject("Rimifon.Upload")
Upload.ReadForm 204800 ' 設置緩沖區大小,Windows 2003默認不允許超過204800,否則會上傳失敗。
Upload.SaveFile ' 保存文件到當前目錄,如需保存到Upload目錄下,則使用SaveFile "Upload"
Upload.Dispose ' 釋放內存
Set Upload = Nothing%>
其它詳細屬性和方法:
Upload.Charset ' 當頁面編碼為utf-8時請設置該值為"UTF-8",默認為"gb2312"
Upload.Filter ' 設置允許上傳的文件類型,默認為「rar|zip|txt|gif|jpg|bmp|htm|html|swf|mht」,為空則允許任何格式文件上傳。
Upload.MaxSize ' 設置單個文件允許上傳的最大大小(Bytes),如不限制則為0(默認為0)
Upload.Field ' 客戶端提交的表單元素;
針對文本元素:Upload.Field.test.Value ' 讀出表單元素"test"的值
針對文件元素:
Upload.Field.MyFile.FilePath ' 讀出文件在客戶端的位置
Upload.Field.MyFile.SourceName ' 讀出文件的源文件名
Upload.Field.MyFile.FileName ' 文件保存在伺服器的文件名
Upload.Field.MyFile.FileSize ' 讀出文件大小
Upload.Field.MyFile.FileType ' 讀出文件的Mime類型
Upload.Field.MyFile.SavePath ' 獲取或設置該文件保存位置的絕對路徑
Upload.Field.MyFile.Message ' 伺服器拒絕該文件時返回的信息
Upload.Field.MyFile.Value ' 文件數據(unicode字元)
存資料庫時直接使用rs("FileData").AppendChunk Upload.Field.MyFile.Value的方式即可
Upload.Form("test") ' 返回表單元素"test"的值,是Upload.Field.test.Value的簡化讀取方式,當不存在"test"元素時返回空值。
Upload.ReadForm FormSize, ProgID
讀取表單,FormSize為緩沖區大小,默認為0,推薦在204800以下(可解決2003下不能上傳大文件的問題)。
提供參數2(ProgID)會為上傳提供進度數據(Application),需要上傳進度條時才用到這個參數。
總文件大小:Application(ProgID + "_TotalBytes")
已上傳數據量:Application(ProgID + "_ReadBytes")
進度條統計完畢後,建議清除這兩個變數:Application.Contents.Remove(ProgID + "_TotalBytes")
Upload.SaveFile SavePath
保存文件到當前目錄的SavePath(該目錄必需存在)下,默認為當前目錄。
Upload.Dispose 釋放內存
附帶代碼:
Test_00.asp vbs(asp) 調用簡單示例
Test_01.asp 指定保存位置測試
2. 請問用ASP如何能實現文件的上傳和下載
<table width=700 align=center><tr><td>
<body topmargin="10" bgcolor="#ffffff">
<%
Dim sStyleID, sUploadDir, sCurrDir, sDir
sPosition = sPosition & "上傳文件管理"
Call Header()
Call Content()
Call Footer()
Sub Content()
If IsObjInstalled("Scripting.FileSystemObject") = False Then
Response.Write "此功能要求伺服器支持文件系統對象(FSO),而你當前的伺服器不支持!"
Exit Sub
End If
' 初始化傳入參數
Call InitParam()
Select Case sAction
Case "DELALL" ' 刪除所有文件
Call DoDelAll()
Case "DEL" ' 刪除指定文件
Call DoDel()
Case "DELFOLDER" ' 刪除文件夾
Call DoDelFolder()
End Select
' 顯示文件列表
Call ShowList()
End Sub
' UploadFile目錄下的所有文件列表
Sub ShowList()
If sCurrDir = "" Then Exit Sub
Response.Write "<table border=1 style='border-collapse: collapse' bordercolor='#C0C0C0' cellpadding=3 cellspacing=0>" & _
"<form action='?id=" & sStyleID & "&dir=" & sDir & "&action=del' method=post name=myform>" & _
"<tr align=center>" & _
"<td width=50 height=25 background='../images/bj5.jpg' style='color:ffffff'>類型</th>" & _
"<td width=140 background='../images/bj5.jpg' style='color:ffffff'>文件地址</th>" & _
"<td width=100 background='../images/bj5.jpg' style='color:ffffff'>大小</th>" & _
"<td width=130 background='../images/bj5.jpg' style='color:ffffff'>最後訪問</th>" & _
"<td width=130 background='../images/bj5.jpg' style='color:ffffff'>上傳日期</th>" & _
"<td width=30 background='../images/bj5.jpg' style='color:ffffff'>刪除</th>" & _
"</tr>"
Dim sCurrPage, nCurrPage, nFileNum, nPageNum, nPageSize
sCurrPage = Trim(Request("page"))
nPageSize = 20
If sCurrpage = "" Or Not IsNumeric(sCurrPage) Then
nCurrPage = 1
Else
nCurrPage = CLng(sCurrPage)
End If
Dim oFSO, oUploadFolder, oUploadFiles, oUploadFile, sFileName
Set oFSO = Server.CreateObject("Scripting.FileSystemObject")
On Error Resume Next
Set oUploadFolder = oFSO.GetFolder(Server.MapPath(sCurrDir))
If Err.Number>0 Then
Response.Write "</table>無效的目錄!"
Exit Sub
End If
If sDir <> "" Then
Response.Write "<tr align=center>" & _
"<td><img border=0 src='sysimage/file/folderback.gif'></td>" & _
"<td align=left colspan=5><a href=""?id=" & sStyleID & "&dir="
If InstrRev(sDir, "/") > 1 Then
Response.Write Left(sDir, InstrRev(sDir, "/") - 1)
End If
Response.Write """>返回上一級目錄</a></td></tr>"
End If
Dim oSubFolder
For Each oSubFolder In oUploadFolder.SubFolders
Response.Write "<tr align=center>" & _
"<td><img border=0 src='sysimage/file/folder.gif'></td>" & _
"<td align=left colspan=4><a href=""?id=" & sStyleID & "&dir="
If sDir <> "" Then
Response.Write sDir & "/"
End If
Response.Write oSubFolder.Name & """>" & oSubFolder.Name & "</a></td>" & _
"<td><a href='?id=" & sStyleID & "&dir=" & sDir & "&action=delfolder&foldername=" & oSubFolder.Name & "'>刪除</a></td></tr>"
Next
Set oUploadFiles = oUploadFolder.Files
nFileNum = oUploadFiles.Count
nPageNum = Int(nFileNum / nPageSize)
If nFileNum Mod nPageSize > 0 Then
nPageNum = nPageNum+1
End If
If nCurrPage > nPageNum Then
nCurrPage = 1
end If
Dim i
i = 0
For Each oUploadFile In oUploadFiles
i = i + 1
If i > (nCurrPage - 1) * nPageSize And i <= nCurrPage * nPageSize Then
sFileName = oUploadFile.Name
Response.Write "<tr align=center>" & _
"<td>" & FileName2Pic(sFileName) & "</td>" & _
"<td align=center><a href=""" & sCurrDir & sFileName & """ target=_blank>"
if right(sFileName,3)="jpg" or right(sFileName,3)="JPG" or right(sFileName,3)="gif" or right(sFileName,3)="GIF" or right(sFileName,3)="BMP" or right(sFileName,3)="bmp" or right(sFileName,3)="png" or right(sFileName,3)="PNG" then
Response.Write "<img border=0 width=80 height=60 src="&sCurrDir& sFileName&" ><BR>"
end if
Response.Write sFileName & "</a></td>" & _
"<td>" & oUploadFile.size & " B </td>" & _
"<td>" & oUploadFile.datelastaccessed & "</td>" & _
"<td>" & oUploadFile.datecreated & "</td>" & _
"<td><input type=checkbox name=delfilename value=""" & sFileName & """></td></tr>"
Elseif i > nCurrPage * nPageSize Then
Exit For
End If
Next
Set oUploadFolder = Nothing
Set oUploadFiles = Nothing
If nFileNum <= 0 Then
Response.Write "<tr><td colspan=6>指定目錄下現在還沒有文件!</td></tr>"
End If
Response.Write "</table>"
If nFileNum > 0 Then
' 分頁
Response.Write "<table border=0 cellpadding=3 cellspacing=0 width='100%'><tr><td align=center>"
If nCurrPage > 1 Then
Response.Write "<a href='?id=" & sStyleID & "&dir=" & sDir & "&page=1'>首頁</a><a href='?id=" & sStyleID & "&dir=" & sDir & "&page="& nCurrPage - 1 & "'>上一頁</a>"
Else
Response.Write "首頁上一頁"
End If
If nCurrPage < i / nPageSize Then
Response.Write "<a href='?id=" & sStyleID & "&dir=" & sDir & "&page=" & nCurrPage + 1 & "'>下一頁</a><a href='?id=" & sStyleID & "&dir=" & sDir & "&page=" & nPageNum & "'>尾頁</a>"
Else
Response.Write "下一頁尾頁"
End If
Response.Write "共<b>" & nFileNum & "</b>個頁次:<b><span class=highlight2>" & nCurrPage & "</span>/" & nPageNum & "</b><b>" & nPageSize & "</b>個文件/頁"
Response.Write "</td></tr></table>"
End If
Response.Write "<p align=center><input type=submit name=b value=' 刪除選定的文件 '> <input type=button name=b1 value=' 清空所有文件 ' onclick=""javascript:if (confirm('你確定要清空所有文件嗎?')) {location.href='admin_uploadfile.asp?id=" & sStyleID & "&dir=" & sDir & "&action=delall';}""></p></form>"
End Sub
' 刪除指定的文件
Sub DoDel()
On Error Resume Next
Dim sFileName, oFSO, sMapFileName
Set oFSO = Server.CreateObject("Scripting.FileSystemObject")
For Each sFileName In Request.Form("delfilename")
sMapFileName = Server.MapPath(sCurrDir & sFileName)
If oFSO.FileExists(sMapFileName) Then
oFSO.DeleteFile(sMapFileName)
End If
Next
Set oFSO = Nothing
End Sub
' 刪除所有的文件
Sub DoDelAll()
On Error Resume Next
Dim sFileName, oFSO, sMapFileName, oFolder, oFiles, oFile
Set oFSO = Server.CreateObject("Scripting.FileSystemObject")
Set oFolder = oFSO.GetFolder(Server.MapPath(sCurrDir))
Set oFiles = oFolder.Files
For Each oFile In oFiles
sFileName = oFile.Name
sMapFileName = Server.MapPath(sCurrDir & sFileName)
If oFSO.FileExists(sMapFileName) Then
oFSO.DeleteFile(sMapFileName)
End If
Next
Set oFile = Nothing
Set oFolder = Nothing
Set oFSO = Nothing
End Sub
' 刪除文件夾
Sub DoDelFolder()
On Error Resume Next
Dim sFolderName, oFSO, sMapFolderName
Set oFSO = Server.CreateObject("Scripting.FileSystemObject")
sFolderName = Trim(Request("foldername"))
sMapFolderName = Server.Mappath(sCurrDir & sFolderName)
If oFSO.FolderExists(sMapFolderName) = True Then
oFSO.DeleteFolder(sMapFolderName)
End If
Set oFSO = Nothing
End Sub
' 檢測伺服器是否支持某一對象
Function IsObjInstalled(strClassString)
On Error Resume Next
IsObjInstalled = False
Err = 0
Dim xTestObj
Set xTestObj = Server.CreateObject(strClassString)
If 0 = Err Then IsObjInstalled = True
Set xTestObj = Nothing
Err = 0
End Function
' 按文件名取圖
Function FileName2Pic(sFileName)
Dim sExt, sPicName
sExt = UCase(Mid(sFileName, InstrRev(sFileName, ".")+1))
Select Case sExt
Case "TXT"
sPicName = "txt.gif"
Case "CHM", "HLP"
sPicName = "hlp.gif"
Case "DOC"
sPicName = "doc.gif"
Case "PDF"
sPicName = "pdf.gif"
Case "MDB"
sPicName = "mdb.gif"
Case "GIF"
sPicName = "gif.gif"
Case "JPG"
sPicName = "jpg.gif"
Case "BMP"
sPicName = "bmp.gif"
Case "PNG"
sPicName = "pic.gif"
Case "ASP", "JSP", "JS", "PHP", "PHP3", "ASPX"
sPicName = "code.gif"
Case "HTM", "HTML", "SHTML"
sPicName = "htm.gif"
Case "ZIP"
sPicName = "zip.gif"
Case "RAR"
sPicName = "rar.gif"
Case "EXE"
sPicName = "exe.gif"
Case "AVI"
sPicName = "avi.gif"
Case "MPG", "MPEG", "ASF"
sPicName = "mp.gif"
Case "RA", "RM"
sPicName = "rm.gif"
Case "MP3"
sPicName = "mp3.gif"
Case "MID", "MIDI"
sPicName = "mid.gif"
Case "WAV"
sPicName = "audio.gif"
Case "XLS"
sPicName = "xls.gif"
Case "PPT", "PPS"
sPicName = "ppt.gif"
Case "SWF"
sPicName = "swf.gif"
Case Else
sPicName = "unknow.gif"
End Select
FileName2Pic = "<img border=0 src='sysimage/file/" & sPicName & "'>"
End Function
' ===============================================
' 初始化下拉框
' v_InitValue : 初始值
' s_Sql : 從資料庫中取值時,select name,value from table
' s_AllName : 空值的名稱,如:"全部","所有","默認"
' ===============================================
Function InitSelect(v_InitValue, s_Sql, s_AllName)
Dim i
InitSelect = ""
If s_AllName <> "" Then
InitSelect = InitSelect & "<option value=''>" & s_AllName & "</option>"
End If
oRs.Open s_Sql, oConn, 0, 1
Do While Not oRs.Eof
InitSelect = InitSelect & "<option value=""" & inHTML(oRs(1)) & """"
If CStr(oRs(1)) = CStr(v_InitValue) Then
InitSelect = InitSelect & " selected"
End If
InitSelect = InitSelect & ">" & outHTML(oRs(0)) & "</option>"
oRs.MoveNext
Loop
oRs.Close
End Function
' ===============================================
' 初始化傳入參數
' ===============================================
Function InitParam()
sStyleID = Trim(Request("id"))
sUploadDir = ""
If IsNumeric(sStyleID) = True Then
sSql = "select S_UploadDir from eWebEditor_Style where S_ID=" & sStyleID
oRs.Open sSql, oConn, 0, 1
If Not oRs.Eof Then
sUploadDir = oRs(0)
End If
oRs.Close
End If
If sUploadDir = "" Then
sStyleID = ""
Else
sUploadDir = Replace(sUploadDir, "\", "/")
If Right(sUploadDir, 1) <> "/" Then
sUploadDir = sUploadDir & "/"
End If
End If
sCurrDir = sUploadDir
' 樣式下的目錄
sDir = Trim(Request("dir"))
If sDir <> "" Then
If CheckValidDir(Server.Mappath(sUploadDir & sDir)) = True Then
sCurrDir = sUploadDir & sDir & "/"
Else
sDir = ""
End If
End If
End Function
' ===============================================
' 檢測目錄的有效性
' ===============================================
Function CheckValidDir(s_Dir)
Dim oFSO
Set oFSO = Server.CreateObject("Scripting.FileSystemObject")
CheckValidDir = oFSO.FolderExists(s_Dir)
Set oFSO = Nothing
End Function
%>
</td></tr></table>
3. asp關於文件的上傳和下載功能
先說明一下:
一、組件上傳,在使用方法上,使用代碼少,更安全,更容易控制,並且功能更強大,比如可以實現控制上傳圖片的尺寸,可以實現真實的上傳進度條等,並且上傳圖片後可以進行對圖片進行編輯等。但缺點是要求服務必需安裝這個組件,如果是自己的伺服器還好辦,如果你是租用空間的話,那麼最好放棄組件上傳吧。
二、無組件上傳,在使用上相對稍復雜一點,需要你到網上下載一個「ASP無組件上傳類」(一般為一個.inc文件),不能對圖片進行處理,只可以控制上傳的文件類型,文件大小等,優點是對伺服器無要求,租用的伺服器空間絕大多數都可以使用。
綜上所述,對於一般小站或企業網站,上傳文件不多,文件不大,要求不高的都使用無組件上傳。
目前較流行的 無組件上傳類有:
化境ASP無組件上傳類
風聲ASP無組件上傳類
艾恩ASP無組件上傳類
等,你在網路里搜索以上關鍵字,找到官方網站,可以免費下載相關上傳類,並且有詳細用法說明,及例子。
4. 求:簡單asp上傳下載代碼
化境ASP無組件上傳 V2.0
上傳至指定文件夾
上傳至資料庫
以上三種需要的話發email給我。
先來一個吧
upfile.asp
----------------------------------------------
<%OPTION EXPLICIT%>
<%Server.ScriptTimeOut=5000%>
<!--#include FILE="upload_5xsoft.inc"-->
<html>
<head>
<title>文件上傳</title>
</head>
<body>
<br>化境文件上傳!<hr size=1 noshadow width=300 align=left><br><br>
<%
dim upload,file,formName,formPath,iCount
set upload=new upload_5xsoft ''建立上傳對象
response.write upload.Version&"<br><br>" ''顯示上傳類的版本
if upload.form("filepath")="" then ''得到上傳目錄
HtmEnd "請輸入要上傳至的目錄!"
set upload=nothing
response.end
else
formPath=upload.form("filepath")
''在目錄後加(/)
if right(formPath,1)<>"/" then formPath=formPath&"/"
end if
iCount=0
for each formName in upload.objForm ''列出所有form數據
response.write formName&"="&upload.form(formName)&"<br>"
next
response.write "<br>"
for each formName in upload.objFile ''列出所有上傳了的文件
set file=upload.file(formName) ''生成一個文件對象
if file.FileSize>0 then ''如果 FileSize > 0 說明有文件數據
file.SaveAs Server.mappath(formPath&file.FileName) ''保存文件
response.write file.FilePath&file.FileName&" ("&file.FileSize&") => "&formPath&File.FileName&" 成功!<br>"
iCount=iCount+1
end if
set file=nothing
next
set upload=nothing ''刪除此對象
Htmend iCount&" 個文件上傳結束!"
sub HtmEnd(Msg)
set upload=nothing
response.write "<br>"&Msg&" [<a href=""javascript:history.back();"">返回</a>]</body></html>"
response.end
end sub
%>
</body>
</html>
upload_5xsoft.inc
<SCRIPT RUNAT=SERVER LANGUAGE=VBSCRIPT>
dim Data_5xsoft
Class upload_5xsoft
dim objForm,objFile,Version
Public function Form(strForm)
strForm=lcase(strForm)
if not objForm.exists(strForm) then
Form=""
else
Form=objForm(strForm)
end if
end function
Public function File(strFile)
strFile=lcase(strFile)
if not objFile.exists(strFile) then
set File=new FileInfo
else
set File=objFile(strFile)
end if
end function
Private Sub Class_Initialize
dim RequestData,sStart,vbCrlf,sInfo,iInfoStart,iInfoEnd,tStream,iStart,theFile
dim iFileSize,sFilePath,sFileType,sFormValue,sFileName
dim iFindStart,iFindEnd
dim iFormStart,iFormEnd,sFormName
Version="化境HTTP上傳程序 Version 2.0"
set objForm=Server.CreateObject("Scripting.Dictionary")
set objFile=Server.CreateObject("Scripting.Dictionary")
if Request.TotalBytes<1 then Exit Sub
set tStream = Server.CreateObject("adodb.stream")
set Data_5xsoft = Server.CreateObject("adodb.stream")
Data_5xsoft.Type = 1
Data_5xsoft.Mode =3
Data_5xsoft.Open
Data_5xsoft.Write Request.BinaryRead(Request.TotalBytes)
Data_5xsoft.Position=0
RequestData =Data_5xsoft.Read
iFormStart = 1
iFormEnd = LenB(RequestData)
vbCrlf = chrB(13) & chrB(10)
sStart = MidB(RequestData,1, InStrB(iFormStart,RequestData,vbCrlf)-1)
iStart = LenB (sStart)
iFormStart=iFormStart+iStart+1
while (iFormStart + 10) < iFormEnd
iInfoEnd = InStrB(iFormStart,RequestData,vbCrlf & vbCrlf)+3
tStream.Type = 1
tStream.Mode =3
tStream.Open
Data_5xsoft.Position = iFormStart
Data_5xsoft.CopyTo tStream,iInfoEnd-iFormStart
tStream.Position = 0
tStream.Type = 2
tStream.Charset ="gb2312"
sInfo = tStream.ReadText
tStream.Close
'取得表單項目名稱
iFormStart = InStrB(iInfoEnd,RequestData,sStart)
iFindStart = InStr(22,sInfo,"name=""",1)+6
iFindEnd = InStr(iFindStart,sInfo,"""",1)
sFormName = lcase(Mid (sinfo,iFindStart,iFindEnd-iFindStart))
'如果是文件
if InStr (45,sInfo,"filename=""",1) > 0 then
set theFile=new FileInfo
'取得文件名
iFindStart = InStr(iFindEnd,sInfo,"filename=""",1)+10
iFindEnd = InStr(iFindStart,sInfo,"""",1)
sFileName = Mid (sinfo,iFindStart,iFindEnd-iFindStart)
theFile.FileName=getFileName(sFileName)
theFile.FilePath=getFilePath(sFileName)
'取得文件類型
iFindStart = InStr(iFindEnd,sInfo,"Content-Type: ",1)+14
iFindEnd = InStr(iFindStart,sInfo,vbCr)
theFile.FileType =Mid (sinfo,iFindStart,iFindEnd-iFindStart)
theFile.FileStart =iInfoEnd
theFile.FileSize = iFormStart -iInfoEnd -3
theFile.FormName=sFormName
if not objFile.Exists(sFormName) then
objFile.add sFormName,theFile
end if
else
'如果是表單項目
tStream.Type =1
tStream.Mode =3
tStream.Open
Data_5xsoft.Position = iInfoEnd
Data_5xsoft.CopyTo tStream,iFormStart-iInfoEnd-3
tStream.Position = 0
tStream.Type = 2
tStream.Charset ="gb2312"
sFormValue = tStream.ReadText
tStream.Close
if objForm.Exists(sFormName) then
objForm(sFormName)=objForm(sFormName)&", "&sFormValue
else
objForm.Add sFormName,sFormValue
end if
end if
iFormStart=iFormStart+iStart+1
wend
RequestData=""
set tStream =nothing
End Sub
Private Sub Class_Terminate
if Request.TotalBytes>0 then
objForm.RemoveAll
objFile.RemoveAll
set objForm=nothing
set objFile=nothing
Data_5xsoft.Close
set Data_5xsoft =nothing
end if
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
End Class
Class FileInfo
dim FormName,FileName,FilePath,FileSize,FileType,FileStart
Private Sub Class_Initialize
FileName = ""
FilePath = ""
FileSize = 0
FileStart= 0
FormName = ""
FileType = ""
End Sub
Public function SaveAs(FullPath)
dim dr,ErrorChar,i
SaveAs=true
if trim(fullpath)="" or FileStart=0 or FileName="" or right(fullpath,1)="/" then exit function
set dr=CreateObject("Adodb.Stream")
dr.Mode=3
dr.Type=1
dr.Open
Data_5xsoft.position=FileStart
Data_5xsoft.to dr,FileSize
dr.SaveToFile FullPath,2
dr.Close
set dr=nothing
SaveAs=false
end function
End Class
</SCRIPT>
5. asp.net如何實現上傳文件到資料庫並下載
網上有不少asp.net上傳下載的代碼,可以參考這里
http://pw.cnblogs.com/archive/2006/05/24/408427.html
主要的一句話在這里:
postedFile.SaveAs(phyPath+fileName);
文件是上傳到伺服器的,不是上傳到資料庫,至於文件路徑,可以記錄到資料庫,也可以直接將鏈接寫到下載點。
6. 怎樣把從網上下下來的ASP代碼上傳到自己的網站上,又怎麼用說的詳細點兒,我是菜鳥。
把自己下載下來的代碼文件放到一個文件夾里.文件夾里目錄結構不要改.
然後用FTP連接你網站虛擬主機,直接把這個文件夾上傳到你空間的目錄里.
如果你原來網站域名是:www.abcd.com
那你這樣就能訪問你上傳的代碼了:www.abcd.com/上傳文件夾名
如果說你不想要原來的代碼,想用新的這個.那你先把原來空間的文件刪除掉.然後把新的這個代碼文件全部上傳到空間的根目錄下.記住,這次不是把所以的代碼文件放到一個文件夾再上傳.而是把把這個文件夾下面的N多個文件傳到空間的根目錄里...文件夾不要
不明白再Q我
7. asp建站如何實現上傳和下載
最好是組件類上傳下載,無組件的不太支持大文件和壓縮類文件。
給你一個最簡單的ASPUPLOAD(網上有下載的)組件上傳代碼。國內空間和伺服器一般都支持ASPUPLOAD
只要你的網頁form ACTION 到這個upload.asp 就上傳了。先建一個upload目錄
這個upload.asp 代碼如下:
<%
Set Upload = Server.CreateObject("Persits.Upload") '重要的:驅動組件
HH_savepath=server.mappath("upload") '定義相對路徑
Upload.Save(HH_savepath) '上傳
%>
三句搞定,簡單吧?哈哈
8. asp.net 2.0中如何實現上傳文件與下載文件 用啥控制項和代碼啊
需用到FileUpLoad控制項
#region 文件上傳
/*
if (FileUpload1.HasFile)
{
string type = FileUpload1.PostedFile.ContentType.ToString();
if (type == "image/pjpeg")
{
string path = Server.MapPath("up/");
FileUpload1.PostedFile.SaveAs(path + FileUpload1.FileName);
Response.Write("<script>alert('上傳成功!');</script>");
}
else
Response.Write("<script>alert('請上傳獲得支持的文件類型!');</script>");
}
else
Response.Write("<script>alert('請選擇您要上傳的文件!');</script>");
//*/
9. asp.net上傳和下載的代碼
太多了 一搜一大堆
ASP.NET上傳和下載文件的代碼
上傳:(可以實現依次上傳多個文件)
前台:
<%@ Register TagPrefix="ew1" Namespace="eWorld.UI" Assembly="eWorld.UI, Version=1.9.0.0, Culture=neutral, PublicKeyToken=24d65337282035f2" %>
<%@ Page language="c#" Codebehind="webNewBid.aspx.cs" AutoEventWireup="false" Inherits="STAR.Web.main.Bidding.WebNewBid" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>WebNewBid</title>
<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
<meta content="C#" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
<LINK href="../../css/style.css" type="text/css" rel="stylesheet">
<script language="JavaScript">
function addFile()
{
var str = '<BR><INPUT type="file" size="50" NAME="File" runat="server">'
document.getElementById('MyFile').insertAdjacentHTML("beforeEnd",str)
}
</script>
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<table class="fullwidth" align="center">
<TR>
<TD vAlign="top">Attachment :</TD>
<TD>
<P id="MyFile"><input id="filMyFile" type="file" size="50" name="filMyFile"><input onclick="addFile()" type="button" value="Add"></P>
<asp:label id="lblAttachmentError" runat="server" ForeColor="Red"></asp:label><BR>
<asp:button id="btnUpload" runat="server" Text="Upload"></asp:button><asp:label id="lblAttachment" runat="server"></asp:label></TD>
</TR>
</table>
</form>
</body>
</HTML>
後台:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Collections.Specialized;
private void btnUpload_Click(object sender, System.EventArgs e)
{
HttpFileCollection files = HttpContext.Current.Request.Files;
for(int i = 0; i < files.Count; i++)
{
if (i<files.Count&&i<10)
{
if (files[i].FileName!="" || files[i] != null)
{
int FileSize = 6*1024*1024;
HttpPostedFile myFile = files[i];
string strFilePath = myFile.FileName.ToString().Trim();
this.lblAttachmentError.Text = "<" + strFilePath + ">"; // Show file name
int nFindSlashPos = strFilePath.Trim().LastIndexOf("\\")+1;
string UploadFileName=strFilePath.Substring(nFindSlashPos);
string FileName=this.txtWorkOrder.Text+"_"+string.Format("{0:yyMMdd-hhmmss}",DateTime.Now)+"_"+UploadFileName;
if(myFile.FileName.Trim() == "") // Empty value in Browse Box
{
this.lblAttachmentError.Text="No file selected.";
return;
}
if(myFile.ContentLength != 0)
{
if (myFile.ContentLength > FileSize)
{
this.lblAttachmentError.Text="File Size is limited to 6 MB only.";
return;
}
this.lblAttachment.Text+= "<BR>"+FileName;
this.lblAttachmentError.Text="";
// string s=this.Request.PhysicalApplicationPath.ToString().Trim();
// string s1=this.Request.ApplicationPath.ToString().Trim();
// string s3=this.Server.MapPath("");
myFile.SaveAs(this.Request.PhysicalApplicationPath.ToString().Trim()+@"\uploads\" + FileName);
ArrayFileName[i]= FileName;
// // return;
}
else
{
this.lblAttachmentError.Text="File not found.";
return;
}
}
}
else
this.lblAttachmentError.Text="Uploaded File exceed limits.";
}
}
下載:(我只給了後太代碼)
public bool ResponseFile(HttpRequest _Request, HttpResponse _Response, string _fileName, string _fullPath, long _speed)
{
try {
FileStream myFile = new FileStream(_fullPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
BinaryReader br = new BinaryReader(myFile);
try {
_Response.AddHeader("Accept-Ranges", "bytes");
_Response.Buffer = false;
long fileLength = myFile.Length;
long startBytes = 0;
Int16 pack = 10240;
Int16 sleep = Convert.ToInt16(Math.Floor(1000 * pack / _speed) + 1);
if (!(_Request.Headers("Range") == null)) {
_Response.StatusCode = 206;
char[] splitChar = new char[1];
splitChar(0) = "=";
splitChar(1) = "-";
string[][0] range = _Request.Headers("Range").Split("a");
startBytes = Convert.ToInt64(range(1));
}
_Response.AddHeader("Content-Length", (fileLength - startBytes).ToString());
if ((startBytes != 0)) {
_Response.AddHeader("Content-Range", string.Format(" bytes {0}-{1}/{2}", startBytes, fileLength - 1, fileLength));
}
_Response.AddHeader("Connection", "Keep-Alive");
_Response.ContentType = "application/octet-stream";
_Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(_fileName, System.Text.Encoding.UTF8));
br.BaseStream.Seek(startBytes, SeekOrigin.Begin);
long maxCount = Convert.ToInt64(Math.Floor((fileLength - startBytes) / pack) + 1);
long i;
for (int i = 0; i <= maxCount; i++) {
if ((_Response.IsClientConnected)) {
_Response.BinaryWrite(br.ReadBytes(pack));
Thread.Sleep(sleep);
} else {
i = maxCount;
}
}
} catch (Exception e) {
return false;
} finally {
br.Close();
myFile.Close();
}
} catch {
return false;
}
}
10. ASP上傳文件代碼
1、2可以解決,3有點復雜,我對無組件上傳也不是很熟悉,很久沒有碰過了,
建議你使用別人做好的上傳類,像無懼上傳類、稻香老農的化境asp無組件上傳類等都可以,有很多,都挺好用的。
關於1、2的問題可參照下面的解決辦法,因為你的代碼全寫在一行,讀起來太麻煩,所以我給拆開了。代碼如下:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>無標題文檔</title>
</head>
<body>
<%
Response.Expires=0
if Request.TotalBytes then
set a=createobject("adodb.stream")
a.Type=1
a.Open
a.write Request.BinaryRead(Request.TotalBytes)
a.Position=0
b=a.Read
c=chrB(13)&chrB(10)
d=clng(instrb(b,c))
e=instrb(d+1,b,c)
set f=createobject("adodb.stream")
f.type=1
f.open
a.Position=d+1
a.to f,e-d-3
f.Position=0
f.type=2
f.CharSet="GB2312"
g=f.readtext
f.Close
h=mid(g,instrRev(g,"\")+1,e)
i=instrb(b,c&c)+4
j=instrb(i+1,b,leftB(b,d-1))-i-2
if j <1 then
set f =nothing
set a =nothing
response.write "未選擇要上傳的文件<a href='?'>重新上傳</a>"
response.end
end if
f.Type=1
f.Open
a.Position=i-1
a.CopyTo f,j
f.SaveToFile server.mappath("/images/"& h),2 '上傳至「/images/」文件夾中
f.Close
set f=Nothing
a.Close
set a=Nothing
response.write "<a href="&Server.URlEncode(h)&">"&h&"</a>"
end if
%>
<script language="javascript">
function checkupload(){
if(document.upload_form.fe.value ==""){
alert("未選擇要上傳的文件");
return false;
}
}
</script>
<form name="upload_form" enctype="multipart/form-data" method="post" onsubmit="return(checkupload())">
<input type="file" name="fe">
<input type="submit" value="上傳" name="B1"></form>
</body>
</html>