Asp下實現多表單域無組件檔案上傳的執行個體

來源:互聯網
上載者:User

最近經常有人問到這類問題,在此轉貼一下,內容:
1。資料庫表結構(Access):
UserID:Text(儲存上傳檔案的使用者ID)
FileContentType:Text(用來儲存上傳檔案的類型,eg:"Application/msword",主要用來使使用者能正確下載此檔案)
FileContent:OLE Object(儲存檔案資料)

2。HTML檔案
muploadfile.htm
<Form name="upload_file" enctype="multipart/form-data" action="muploadfile.asp" method=post>
<input type=hidden name="UserID" value="abc">
<input type=hidden name="FileUploadStart"> '這裡用來表示開始檔案資料上傳
File to send:

<INPUT TYPE="file" name="file_up" size="30"><br>
<INPUT TYPE="file" name="file_up" size="30"><br>
<input type=hidden name="FileUploadEnd"> '這裡用來表示檔案資料結束
<input type=submit value=Submit>
</Form></P><P>
3。ASP檔案
muploadfile.asp</P><P><%
Response.Expires=0
Function bin2str(binstr)
Dim varlen,clow,ccc,skipflag </P><P> 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 </P><P>
varByteCount = Request.TotalBytes
bnCRLF = chrB( 13 ) & chrB( 10 )
binHTTPHeader=Request.BinaryRead(varByteCount)
Divider = LEFTB( binHTTPHeader, INSTRB( binHTTPHeader, bnCRLF ) - 1 )</P><P>'開始讀非檔案域的資料
Do while lenB(binHTTPHeader)>46

binHeaderData = LeftB(binHTTPHeader, INSTRB( binHTTPHeader, bnCRLF & bnCRLF )-1)
strHeaderData=bin2str(binHeaderData)</P><P> 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)

'判斷檔案資料時候開始
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</P><P> binFieldValue=MIDB( binHTTPHeader, DataStart, DataEnd )
strFieldValue=bin2str(binFieldValue)
strFieldValue=Trim(strFieldValue)
strFieldValue=Replace(strFieldValue,vbcrlf,vbnullstring)</P><P> '非檔案上傳域變數賦值
execute strFieldName&"="""&strFieldValue&""""

binHTTPHeader=MIDB(binHTTPHeader,INSTRB( DataStart + 1, binHTTPHeader, divider ))

loop</P><P>'開始處理檔案資料
Do while lenB(binHTTPHeader)>46

binHeaderData = LeftB(binHTTPHeader, INSTRB( binHTTPHeader, bnCRLF & bnCRLF )-1)

strHeaderData=bin2str(binHeaderData)

'讀取上傳檔案的Content-Type
lngFileContentTypeStart=Instr(strHeaderData,"Content-Type:")+Len("Content-Type:")
strFileContentType=Trim(Mid(strHeaderData,lngFileContentTypeStart))
strFileContentType=Replace(strFileContentType,vbCRLF,vbNullString)

'讀取上傳的檔案名稱
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)

'讀取上傳檔案資料
DataStart = INSTRB( binHTTPHeader, bnCRLF & bnCRLF ) + 4
DataEnd = INSTRB( DataStart + 1, binHTTPHeader, divider ) - DataStart

If strFileName<>"" Then

binFieldValue=MIDB( binHTTPHeader, DataStart, DataEnd )

'將上傳的檔案寫入資料庫
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。下載使用者上傳的檔案
<%
Response.Buffer = true
Response.Clear

UserID=request("UserID")</P><P>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")</P><P>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
%></P><P>就是這些了,希望此方法對大家能有所協助。:)

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.