AspUpload 是經典 ASP 時代最常用的上傳元件。本文提供完整上傳程式碼,含副檔名白名單驗證、檔案大小限制、重新命名避免覆蓋、錯誤處理。
regsvr32 AspUpload.dll (需管理員權限)IUSR/IIS_IUSRS 寫入權限<%
Set Upload = Server.CreateObject("Persits.Upload.1")
If Upload Is Nothing Then
Response.Write "AspUpload 元件未安裝"
Response.End
End If
' 基本設定
Upload.SetMaxSize 10 * 1024 * 1024 ' 限制 10MB
Upload.OverwriteFiles = False ' 不覆蓋同名檔案
Upload.CodePage = 65001 ' UTF-8
' 副檔名白名單 (安全關鍵)
allowedExt = "jpg,jpeg,png,gif,bmp,pdf,doc,docx,xls,xls,txt,zip,rar"
Upload.AllowedExtensions = allowedExt
' 執行上傳
count = Upload.Save(Server.MapPath("/uploads/"))
If count = 0 Then
Response.Write "無檔案上傳或上傳失敗:" & Upload.Error
Else
For Each File In Upload.Files
Response.Write "檔案:" & File.FileName & "
"
Response.Write "儲存為:" & File.Path & "
"
Response.Write "大小:" & File.Size & " bytes
"
Response.Write "Content-Type:" & File.ContentType & "
"
Next
End If
%>
<form action="upload.asp" method="post" enctype="multipart/form-data">
<input type="file" name="myFile">
<input type="submit" value="上傳">
</form>
File.SaveAs Server.MapPath("/uploads/") & "\" & Year(Now)&Month(Now)&Day(Now)&Hour(Now)&Minute(Now)&Second(Now) & "_" & Rnd() & "." & File.Extweb.config 禁用腳本執行:<configuration>
<system.webServer>
<handlers accessPolicy="Read" />
</system.webServer>
</configuration>
..、/、\,AspUpload 內建防護但建議再次檢查。| 錯誤 | 原因 | 解決 |
|---|---|---|
| 元件未安裝/建立失敗 | DLL 未註冊、位元數不符、權限不足 | regsvr32、啟用 32 位應用程式池、給 IUSR 讀取 DLL 權限 |
| 上傳大小限制 | IIS maxAllowedContentLength、ASP AspMaxRequestEntityAllowed | 修改 applicationHost.config 或 metabase.xml |
| 中文檔名亂碼 | CodePage 未設為 65001 | Upload.CodePage = 65001 |
| 檔案 0 bytes | 表單缺 enctype="multipart/form-data" | 補上 enctype |