asp 檔案上傳舉例

來源:互聯網
上載者:User

<%@LANGUAGE="VBSCRIPT" CODEPAGE="936"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title></title>
<style>
td{font-size:12px;}
</style>
</head>

<body>
<TABLE border=0 cellpadding=0 cellspacing=0 width=400>
<form id="form1" name="form1" method="post" action="2.asp教程" onsubmit="return f()"> 
<TR>
 <TD colspan=2 align="center"><b></b></TD>
</TR>
<TR>
 <TD>標題</TD>
 <TD><input type="text" name="title"></TD>
</TR>
<TR>
 <TD>檔案</TD>
 <TD><input name="pic" type="text" id="pic" size="30" /><input type="button" name="Submit" value="瀏覽.." onclick="window.open('upload.asp?fuptype=pic&frmname=form1&bdname=pic');"/></TD>
</TR>
<TR>
 <TD colspan=2 align="center"><input type="submit" value="儲存" style="width:150px;"></TD>
</TR>
</form>
</TABLE>
<script language="javascript教程">
function f()
{
    if(document.form1.title.value=="")
 {
    alert("標題不可為空");
    return false;
 }
 if(document.form1.pic.value=="")
 {
    alert("圖片不可為空");
    return false;
 }
}
</script>
</body>
</html>


upload.asp檔案

<form method="POST" enctype="multipart/form-data" action="?act=upload">
<input type="file" size="20" name="file1"><input type="submit" value="上傳">
</form>
<%
if request("act") = "upload" then

'****************************************
'  功能:aspUpload有組件上傳檔案
'  作者:wangsdong
'  網址:www.111cn.net教程
'  原創源碼教程,轉載請保留此資訊,謝謝
'****************************************

AllowExt = "jpg,png,gif,zip,rar,sql,txt,bak"
FileSize=4194304
'On Error Resume Next

' 建立AspUpload對象
Set Upload = Server.CreateObject("Persits.Upload")

' 限制檔案大小
Upload.SetMaxSize FileSize, True

' 上傳路徑--目前的目錄下的test目錄
if session("fuptype")="pic" then
 path="images/pic"
else
 path="images/test"
end if
uploadDir = Server.MapPath(path)
AutoCreateFolder(uploadDir) '建立檔案夾

' 嘗試建立路徑檔案夾,true表示忽略目錄已存在錯誤
'Upload.CreateDirectory uploadDir, true

' 先上傳檔案至伺服器記憶體
Count = Upload.Save()

' 檢測上傳錯誤
If Err.Number = 8 Then
Response.Write chinese2unicode("錯誤: 檔案過大!")
Response.end
Else
If Err <> 0 Then
response.write chinese2unicode("發生錯誤:")
response.write chinese2unicode(Err.Description)
response.end
End If
End If

'Response.Write chinese2unicode("共 " & Count & " 個檔案") & "<br><br>"

' 指定一個上傳的表單檔案
Set File = Upload.Files("file1")
If Not File Is Nothing Then
' 擷取原本檔案名稱
'Filename = File.Filename '如果使用原檔案名稱,請去掉前面的單引號
filename=replace(replace(replace(now()," ",""),"-",""),":","")&File.Ext '以時間為檔案名稱
' 擷取副檔名
Fileext = File.Ext
v=path&"/"&filename

' 檢測檔案格式是否合格
ChkStr = ","&Lcase(AllowExt)&","
If Instr(ChkStr,","&right(Fileext,3)&",") <= 0 Then
Response.Write chinese2unicode("錯誤: 檔案類型不正確!")
response.write "<br>"
response.write chinese2unicode("只允許:"&AllowExt)
' 刪除記憶體中的臨時檔案,以釋放記憶體或硬碟空間(還可用Copy、Move兩個指令)
File.Delete
' 檢測是否存在檔案
elseif Upload.FileExists(uploadDir & "" & Filename) Then
File.SaveAs uploadDir & "" & Filename
Response.Write chinese2unicode("已覆蓋存在相同檔案名稱的檔案: ") & File.Path
' 儲存檔案
else
File.SaveAs uploadDir & "" & Filename
'Response.Write chinese2unicode("檔案已儲存到: ") & File.Path
'v=Replace(UploadFilePath&file.filename,"../","")   
  response.write "<script>opener.document."&session("frmname")&"."&session("bdname")&".value='"&v&"';window.close();</script>"
end If
Else
Response.Write chinese2unicode("錯誤: 您並沒有選擇檔案!")
End If

else
   session("fuptype")=request("fuptype")  '上傳類型
   session("frmname")=request("frmname")  'form名
   session("bdname")=request("bdname")         '表單名
end If

' gb2312轉unicode,解決中文亂碼問題
function chinese2unicode(Str)
dim i
dim Str_one
dim Str_unicode
for i=1 to len(Str)
Str_one=Mid(Str,i,1)
Str_unicode=Str_unicode&chr(38)
Str_unicode=Str_unicode&chr(35)
Str_unicode=Str_unicode&chr(120)
Str_unicode=Str_unicode& Hex(ascw(Str_one))
Str_unicode=Str_unicode&chr(59)
next
Response.Write Str_unicode
end function
'--------------------------------
'自動建立指定的多級檔案夾
'strPath為絕對路徑
Function AutoCreateFolder(strPath) 'As Boolean
        On Error Resume Next
        Dim astrPath, ulngPath, i, strTmpPath
        Dim objFSO
        If InStr(strPath, "") <=0 or InStr(strPath, ":") <= 0 Then
                AutoCreateFolder = False
                Exit Function
        End If
        Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
        If objFSO.FolderExists(strPath) Then
                AutoCreateFolder = True
                Exit Function
        End If
        astrPath = Split(strPath, "")
        ulngPath = UBound(astrPath)
        strTmpPath = ""
        For i = 0 To ulngPath
                strTmpPath = strTmpPath & astrPath(i) & ""
                If Not objFSO.FolderExists(strTmpPath) Then
                        '建立
                        objFSO.CreateFolder(strTmpPath)
                End If
        Next
        Set objFSO = Nothing
        If Err = 0 Then
                AutoCreateFolder = True
        Else
                AutoCreateFolder = False
        End If
End Function  
%>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.