1. Database table structure (Access):
The userid: Text (user ID) to save the uploaded file
On FileContentType: Text (used to save the uploaded file type, eg: “Application / msword”, used primarily to keep users can download this file correctly)
FileContent: OLE Object (save the file data)
2. HTML file
muploadfile.htm
<Form Name=”upload_file” enctype=”multipart/form-data” action=”muploadfile.asp” method=post>
<input type=hidden name=”UserID” value=”abc”>
the <input type=hidden name=”FileUploadStart”> ‘is used to indicate the start file data upload
File to send: <BR>
<INPUT TYPE=”file” name=”file_up” size=”30″> <br>
<INPUT TYPE=”file” name=”file_up” size=”30″> <br>
<input type=hidden name=”FileUploadEnd”> ‘is used to indicate the end of the file data
<input type=submit value=Submit>
</ Form>
3. ASP file
muploadfile.asp
<%
Response.Expires = 0
Function bin2str (binstr)
Dim varlen, clow, ccc, skipflag
skipflag = 0
ccc = “”
If Not IsNull (binstr) Then
varlen = LenB (binstr)
For i = 1 To varlen
If skipflag = 0 Then
clow = MidB (binstr, i, 1)
If AscB (clow)> 127 Then
ccc = ccc & Chr (AscW (MidB (binstr, i +1,1) & clow))
skipflag = 1
Else
ccc = ccc & Chr (AscB (clow))
End If
Else
skipflag = 0
End If
Next
End If
bin2str = CCC
End Function
varByteCount = Request.TotalBytes
bnCRLF = chrB (13) & chrB (10)
binHTTPHeader = Request.BinaryRead (varByteCount)
Divider = LEFTB (binHTTPHeader, INSTRB (binHTTPHeader, bnCRLF) – 1)
‘Began to read the data of the non-paper domain
Do while lenB (binHTTPHeader)> 46
binHeaderData = LeftB (binHTTPHeader, INSTRB (binHTTPHeader, bnCRLF & bnCRLF) -1)
strHeaderData = bin2str (binHeaderData)
lngFieldNameStart = Instr (strHeaderData, “name =” & chr (34)) + Len (“name =” & chr (34))
lngFieldNameEnd = Instr (lngFieldNameStart, strHeaderData, chr (34))
strFieldName = Mid (strHeaderData, lngFieldNameStart, lngFieldNameEnd-lngFieldNameStart)
strFieldName = Trim (strFieldName)
strFieldName = Replace (strFieldName, vbcrlf, vbnullstring)
‘Judgment file data when
If strComp (strFieldName, “FileUploadStart”, 1) = 0 Then
binHTTPHeader = MIDB (binHTTPHeader, INSTRB (DataStart + 1, binHTTPHeader, divider))
exit do
End if
DataStart = INSTRB (binHTTPHeader, bnCRLF & bnCRLF) + 4
DataEnd = INSTRB (DataStart + 1, binHTTPHeader, divider) – DataStart
binFieldValue = MIDB (binHTTPHeader, DataStart, DataEnd)
strFieldValue = bin2str (binFieldValue)
strFieldValue = Trim (strFieldValue)
strFieldValue = Replace (strFieldValue, vbcrlf, vbnullstring)
‘Non-file upload field variable assignment
execute strFieldName & “=” “” & strFieldValue & “” “”
binHTTPHeader = MIDB (binHTTPHeader, INSTRB (DataStart + 1, binHTTPHeader, divider))
loop
‘Start processing document data
Do while lenB (binHTTPHeader)> 46
binHeaderData = LeftB (binHTTPHeader, INSTRB (binHTTPHeader, bnCRLF & bnCRLF) -1)
strHeaderData = bin2str (binHeaderData)
Content-Type ‘read uploaded file
lngFileContentTypeStart = Instr (strHeaderData, “Content-Type:”) + Len (“Content-Type:”)
strFileContentType = Trim (Mid (strHeaderData, lngFileContentTypeStart))
strFileContentType = Replace (strFileContentType, vbCRLF, vbNullString)
‘Read uploaded file name
lngFileNameStart = Instr (strHeaderData, “filename =” & chr (34)) + Len (“filename =” & chr (34))
lngFileNameEnd = Instr (lngFileNameStart, strHeaderData, chr (34))
strFileName = Mid (strHeaderData, lngFileNameStart, lngFileNameEnd-lngFileNameStart)
strFileName = Trim (strFileName)
strFileName = Replace (strFileName, vbCRLF, vbNullString)
‘Read upload file data
DataStart = INSTRB (binHTTPHeader, bnCRLF & bnCRLF) + 4
DataEnd = INSTRB (DataStart + 1, binHTTPHeader, divider) – DataStart
If strFileName <> “” Then
binFieldValue = MIDB (binHTTPHeader, DataStart, DataEnd)
‘Uploaded files written to the database
Set Conn = Server.CreateObject (“ADODB.Connection”)
conn.Open “DSN = abc”
SQL = “select * from User_File”
set rs = Server.CreateObject (“ADODB.Recordset”)
rs.Open sql, conn, 3,3
rs.addnew
rs (“userid”) = userid
rs (“FileContentType”) = strFileContentType
rs (“FileContent”). AppendChunk binFieldValue
rs.update
rs.close
Set rs = Nothing
conn.Close
Set conn = Nothing
End if
binHTTPHeader = MIDB (binHTTPHeader, INSTRB (DataStart + 1, binHTTPHeader, divider))
loop
%>
4. Download the user uploaded files
<%
Response.Buffer = true
Response.Clear
Userid = request (“userid”)
Set conn = Server.CreateObject (“ADODB.Connection”)
set rs = Server.CreateObject (“ADODB.Recordset”)
conn.open “DSN = UploadFile”
rs.open “select * from User_File where UserID = ‘” & UserID & “‘”, conn, 3,3
Response.ContentType = rs (“FileContentType”)
lngOffset = 0
conChunkSize = 1024
lngPictSize = rs (“FileContent”). ActualSize
Do While lngOffset <lngPictSize
varChunk = rs (“FileContent”). GetChunk (conChunkSize)
Response.BinaryWrite varChunk
lngOffset = lngOffset + conChunkSize
If lngOffset> lngPictSize Then Exit Do
Loop
rs.close
Set rs = Nothing
conn.close
set conn = nothing
%>
That, I hope this method can be helpful to everyone. 🙂