ASP Upload 使用說明

來源:互聯網
上載者:User
使用ASP實現檔案上傳到WEB伺服器
ASPupload 2.0版,相關源檔案如下(uploadTest.htm):

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>檔案上傳用戶端</title>
</head>
<body>
<form method="POST" action="uploadTest1.asp" enctype="multipart/form-data"
name="UPloadForm">
<p><input type="file" Name="File1"> </p>
<p><input type="submit" value="Submit" name="Upload"></p>
</form>
</body>
</html>
其中用戶端檔案要注意兩點:
* 檔案上傳提交表單(Form)的enctype必須指定為“multipart/form-data”
* 語句<input type="file" Name="File1">表示上傳檔案域,使用者可以在該域中輸入或選定檔案。
伺服器端源檔案如下(uploadTest1.asp):
<%response.buffer=true%>
<html>
<%Set Upload=Server.createobject("Persits.Upload.1") '建立檔案上傳組件
Count=Upload.Save("e:\aspupload") '將用戶端檔案儲存到WEB伺服器端的本地硬碟上%>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>Upload Test</title>
<meta name="GENERATOR" content="Microsoft FrontPage 3.0">
</head>
<body>
<p>上傳了<%=Count%>個檔案</p>
<p>File has been uploaded.</p>
</body>
</html>
其中,指令碼Set Upload=Server.createobject("Persits.Upload.1")建立了檔案上傳組件,該組件通過調用Save方法將瀏覽器端的檔案內容儲存到指定路徑。

將檔案存在資料庫中
將檔案儲存在資料庫中主要用了ASPUpLoad組件中檔案對象的ToDatabase方法。源檔案如下:
用戶端源檔案(uploadToDB.htm):
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>檔案上傳用戶端</title>
</head>
<body>
<form method="POST" action="UploadToDB.asp" enctype="multipart/form-data"
name="FormUpload">
<p><input type="file" name="FileUpload"> </p>
<p><input type="submit" value="上傳" name="B1"></p>
</form>
<p><a href="readFile.asp">讀取資料庫中檔案</a></p>
</body>
</html>
伺服器端源檔案(uploadToDB.asp):
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>檔案上傳到資料庫</title>
</head>
<body>
<%Set Upload=Server.createobject("Persits.Upload.1")
Count=Upload.Save("e:\aspupload")
on error resume next
set FileObj=Upload.Files("FileUpload")
SQL="insert into upLoadFile (Title,FileName,Content) values ('"&FileObj.Path&"','"&FileObj.Path&"',?)"
response.write SQL
FileObj.ToDatabase "DSN=FileUpload;UID=sa;",SQL
if Err<>0 then
Response.write "Error Saving the file:"&Err.Description
else
FileObj.delete
response.write "Success!"
end if
%>
</body>
</html>

從資料庫中讀取檔案內容並發送給用戶端瀏覽器
從資料庫中讀取內容在發送給瀏覽器之前,首先必須讓瀏覽器知道內容的資料類型,這通過向用戶端發送ContentType描述實現。為簡單起見,這裡假設發送的內容是Word文檔,並且顯示最新插入的記錄。源檔案如下:
用戶端源檔案為uploadToDB.htm(同上一部分的用戶端檔案)。
伺服器端源檔案(readFile.asp):

<%Response.Expires = 0
response.buffer=true%>
<%response.clear
Response.ContentType = "application/msword"
set conn=server.createobject("adodb.connection")
conn.open "DSN=FileUpload;UID=sa;"
set rs1=conn.execute("select maxid=max(id) from uploadFile")
SQL="select * from uploadFile where id="&rs1("maxid")
set rs=conn.execute(SQL)
Response.BinaryWrite rs("Content")
rs.close
rs1.close
conn.close
Response.End
%>
其中,Web Server向用戶端發送Content-Type="application/msword",使用戶端認為這是Word文檔,然後伺服器從資料庫中讀取檔案內容(為簡單起見,假定是資料庫中最後一條記錄),然後以二進位流的方式向用戶端發送(調用ASP內建對象Response的BinaryWrite方法)。當用戶端接收到這些內容後便自動啟動Word OLE服務,使Word控制項嵌在瀏覽器IE中將收到的內容格式化顯示。

相關文章

聯繫我們

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