不安裝任何 COM 元件,純 VBScript 實作 multipart/form-data 解析與檔案儲存,支援大檔、多檔、類型/大小限制、自動/手動儲存模式、圖片尺寸偵測。本文整理精簡版使用範例與核心類別原始碼。
<form action="upload.asp" enctype="multipart/form-data" method="post">
產品圖:<input name="strPhoto2" type="file" />
<input type="submit" value="上傳" />
<input type="reset" value="重置" /> (限制 < 500KB)
</form>
<%@ LANGUAGE="VBSCRIPT" CODEPAGE="936" %>
<% Option Explicit : Server.ScriptTimeout = 5000 %>
<!--#include file="UpLoadClass.asp"-->
<%
Dim up
Set up = New UpLoadClass
' 1. 手動儲存模式 (預設自動儲存,改為 2 代表手動控制)
up.AutoSave = 2
' 2. 字元集 (預設 gb2312,若網頁為 UTF-8 請設為 "UTF-8")
' up.Charset = "UTF-8"
' 3. 儲存路徑 (相對網站根目錄,預設 UpLoadFile/,需有寫入權限)
' up.SavePath = "UpLoadFile/"
up.Open() ' 解析 Request 二進位資料
' 4. 針對單一欄位設定限制
up.MaxSize = 500 * 1024 ' 500 KB
up.FileType = "jpg/gif/bmp/png" ' 允許副檔名,不分大小寫
' 5. 執行儲存:Save(表單欄位名, 索引) 索引 0 代表第一個檔案
If up.Save("strPhoto2", 0) Then
Response.Write "上傳成功!路徑:" & up.SavePath & up.Form("strPhoto2") & "
"
' 若為圖片,可取得寬高
' If up.Form("strPhoto2_Width") <> "" Then
' Response.Write "寬:" & up.Form("strPhoto2_Width") & " 高:" & up.Form("strPhoto2_Height")
' End If
Else
Response.Write "上傳失敗:" & up.Error & "
"
End If
Set up = Nothing
%>
<%
Class UpLoadClass
Private m_TotalSize, m_MaxSize, m_FileType, m_SavePath, m_AutoSave, m_Error, m_Charset
Private m_dicForm, m_binForm, m_binItem, m_strDate, m_lngTime
Public FormItem, FileItem
Public Property Get Version : Version = "Fonshen UpLoadClass 2.1" : End Property
Public Property Get Error : Error = m_Error : End Property
Public Property Let Charset(v) : m_Charset = v : End Property
Public Property Get Charset : Charset = m_Charset : End Property
Public Property Let MaxSize(v) : If IsNumeric(v) Then m_MaxSize = CLng(v) : End Property
Public Property Get MaxSize : MaxSize = m_MaxSize : End Property
Public Property Let FileType(v) : m_FileType = v : End Property
Public Property Get FileType : FileType = m_FileType : End Property
Public Property Let SavePath(v) : m_SavePath = Replace(v, "\", "/") : End Property
Public Property Get SavePath : SavePath = m_SavePath : End Property
Public Property Let AutoSave(v) : m_AutoSave = CInt(v) : End Property
Public Property Get AutoSave : AutoSave = m_AutoSave : End Property
Private Sub Class_Initialize()
m_Charset = "gb2312"
m_AutoSave = 1
m_SavePath = "UpLoadFile/"
Set m_dicForm = Server.CreateObject("Scripting.Dictionary")
End Sub
Public Sub Open()
' 解析 Request.TotalBytes 二進位資料,填入 m_dicForm (表單欄位) 與 FileItem (檔案資訊)
' 此處省略完整解析邏輯 (約 200 行),完整版請參考原作者網站 https://www.fonshen.com
' 關鍵步驟:
' 1. Request.BinaryRead(Request.TotalBytes) 取得原始位元組
' 2. 依 boundary 切割 part
' 3. 解析 header 取得 name、filename、Content-Type
' 4. 一般欄位存入 m_dicForm,檔案存入 FileItem 陣列 (含二進位內容)
End Sub
Public Function Save(fieldName, index)
' 依 m_AutoSave 模式決定自動/手動儲存
' 檢查檔案大小、副檔名、儲存目錄權限
' 寫入檔案 (ADODB.Stream Type=1 二進位寫入)
' 成功回傳 True,失敗設定 m_Error 回傳 False
Save = False ' 精簡版略過實作
End Function
End Class
%>
| 功能 | 說明 |
|---|---|
| 無需註冊元件 | 純 VBScript,虛擬主機也能用 |
| 手動/自動儲存 | AutoSave=1 解析即存檔;=2 需呼叫 Save() 彈性控制路徑/檔名 |
| 大小/類型限制 | MaxSize (位元組)、FileType (副檔名清單) |
| 圖片尺寸偵測 | 支援 JPG/GIF/PNG/BMP,解析後自動填入 欄位名_Width/_Height |
| 多檔上傳 | 同一欄位多檔案以索引區分,Save(field, index) |
| 字元集支援 | 預設 gb2312,可設 Charset="UTF-8" 處理中文檔名 |
UpLoadFile/) 已給 IIS 執行身分 **寫入** 權限enctype="multipart/form-data" 且 method="post"Server.ScriptTimeout 足夠大 (大檔建議 300 秒以上)UpLoadClass.asp 以 UTF-8 無 BOM 或指定 CodePage 儲存風聲上傳類是 ASP 經典免元件上傳方案的代表作,原始碼開放、邏輯清晰、功能完整。理解其 Open() 解析二進位流與 Save() 寫入檔案的核心流程,有助於在無法安裝元件的受限環境下快速構建上傳功能。