⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 manual_db.html

📁 aspupload
💻 HTML
📖 第 1 页 / 共 2 页
字号:
	<P>

	<TABLE BORDER=1 CELLSPACING=0 CELLPADDING=3>
	<TR><TD BGCOLOR="#FFFF00">
	<FONT SIZE="1" FACE="Courier New">
	&lt;HTML><BR>
	&lt;BODY><BR>

	&lt;%<BR>
	' Build ODBC connection string<BR>
	Connect = "Driver={Microsoft Access Driver (*.mdb)};DBQ=" & Server.MapPath(".\aspupload.mdb")<P>
	
	' For SQL Server use a string simlar to this:<BR>
	' Connect = "Driver=SQL Server;Server=MYSERVER;UID=sa;PWD=xxxxx"<BR>
	Set rs = Server.CreateObject("adodb.recordset")<P>

	Set Upload = Server.CreateObject("Persits.Upload")<P>
	
	' Open MYIMAGES table<BR>
	rs.Open "MYIMAGES", Connect, 2, 3<P>

	' Scroll through records<BR>
	Count = 0<BR>
	While Not rs.EOF<BR>
	&nbsp;&nbsp;&nbsp;Filename = Trim(rs("filename"))<P>
		
	&nbsp;&nbsp;&nbsp;SQL = "select image_blob from myimages where id=" & rs("id")<BR>
	&nbsp;&nbsp;&nbsp;Upload.FromDatabase Connect, SQL, "c:\upload\" & Filename<BR>

	&nbsp;&nbsp;&nbsp;Response.Write "File exported: c:\upload\" & Filename & "&lt;BR>"<BR>
	&nbsp;&nbsp;&nbsp;Count = Count + 1<BR>

	&nbsp;&nbsp;&nbsp;rs.MoveNext<BR>
	Wend<P>

	Response.Write "&lt;P>" & Count & " files exported."<BR>
	%><BR>
	&lt;/BODY><BR>
	&lt;/HTML><BR>
	</FONT></TD></TR>
	</TABLE>
	<P>
	Here, we have to employ ADO to scroll through all the records of our sample table
	and call FromDatabase on each individual record.
	<P>
	<P>
	Downloading a file from the database directly to a client browser
	is possible without using AspUpload or any other third-party component,
	it can be achieved with ADO alone. This will be covered later in this chapter.
	<P>
	Click the link below to run this code sample:
	<P>
	<B><A TARGET="_new" HREF="http://localhost/aspupload/04_db/odbcexport.asp">http://localhost/aspupload/04_db/odbcexport.asp</A></B>
	&nbsp;<A HREF="javascript:;" OnClick="open('helppopup.html','','width=400,height=400');"><IMG SRC="help.gif" BORDER="0" ALT="Why is this link not working?"></A>

	</BLOCKQUOTE>
	<B><FONT COLOR="#0000A0">ADO-based File Uploads</FONT></B>
	<BLOCKQUOTE>
	File.ToDatabase is not the only way to save files in the database
	with AspUpload. Instead of ODBC, you can use the ADO Recordset object
	in conjunction with the <B>File.Binary</B> property. Using the <B>Binary</B> property,
	a file can be assigned to the recordset much the same way as a regular numeric or string value,
	for example:
	<P>
	<FONT SIZE="2" FACE="Courier New"><B>
	rs.Open "MYIMAGES", Connect, 2, 3<BR>
	rs.AddNew<BR>
	Set File = Upload.Files("FILE1")<BR>
	<FONT COLOR="#FF0000">rs("image_blob").Value = File.Binary</FONT><BR>
	rs("filename").Value = File.FileName<BR>
	rs("filesize").Value = File.Size<BR>
	rs.Update<BR>
	</B></FONT>
	<P>
	This code is more intuitive as it does not require building complex
	SQL statements. Instead, the traditional ADO objects are used. This approach
	works equally well for inserts and updates.
	<P>
	If the ultimate destination of a file is the database, you may consider
	using uploads to memory (described in the <A HREF="manual_memory.html">previous chapter</A>)
	for better security and performance. The sample files <B>ado.asp</B>
	and <B>ado_upload.asp</B> demonstrate the combined use of memory uploads and File.Binary
	for saving files in the database.
	<P>
	This code sample also utilizes <B>one-way hashing</B> to determine whether a newly
	uploaded file already exists in the database. A one-way hash function
	is an algorithm which uses a variable-length input such as an arbitrary file
	or text string, and produces a fixed-length output (128 bit or 160 bit
	for the hash algorithms <B>MD5</B> and <B>SHA1</B>, respectively). 
	AspUpload offers MD5 hash value computation via the property <B>File.MD5Hash</B>.
	<P>
	The term "one-way" is used because it is
	practically impossible to come up with an input which would
	produce a given hash value. Also, it is impossible
	to come up with two different documents which would hash to the same value.
	This remarkable feature of the one-way hash function can be used
	to determine whether a given document already exists in the database.
	We store each file's hash value along with other information,
	and before adding a new file to the database, we look up its hash value.
	If the value is already in the database, we do not save the file.
	<P>
	<TABLE BORDER=1 CELLSPACING=0 CELLPADDING=3>
	<TR><TD BGCOLOR="#FFFF00">
	<FONT SIZE="1" FACE="Courier New">
	&lt;HTML><BR>
	&lt;BODY><BR>

	&lt;%<BR>
	Set Upload = Server.CreateObject("Persits.Upload")<P>

	' we use memory uploads, so we must limit file size<BR>
	Upload.SetMaxSize 100000, True<P>

	' Save to memory. Path parameter is omitted<BR>
	Count = Upload.Save<P>

	' Obtain file object<BR>
	Set File = Upload.Files("THEFILE")<P>

	If Not File Is Nothing Then<BR>
	&nbsp;&nbsp;&nbsp;' Build ADO connection string<BR>
	&nbsp;&nbsp;&nbsp;Connect = "Driver={Microsoft Access Driver (*.mdb)};DBQ=" & Server.MapPath(".\aspupload.mdb")<BR>

	&nbsp;&nbsp;&nbsp;' If you use SQL Server, the connecton string may look like this:<BR>
	&nbsp;&nbsp;&nbsp;' Connect = "Provider=SQLOLEDB;Server=SRV;Database=mydb;UID=sa;PWD=xxx"<BR>

	&nbsp;&nbsp;&nbsp;' Use ADO Recordset object<BR>
	&nbsp;&nbsp;&nbsp;Set rs = Server.CreateObject("adodb.recordset")<P>

	&nbsp;&nbsp;&nbsp;' Optional: check whether this file already exists using MD5 hash<BR>
	&nbsp;&nbsp;&nbsp;Hash = File.MD5Hash<BR>
	&nbsp;&nbsp;&nbsp;rs.Open "SELECT * from MYIMAGES WHERE Hash='" & Hash & "'", Connect, 2, 3<BR>
	&nbsp;&nbsp;&nbsp;If Not rs.EOF Then<BR>
	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Response.Write "This file already exists in the database."<BR>
	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Response.End<BR>
	&nbsp;&nbsp;&nbsp;End If<BR>
	&nbsp;&nbsp;&nbsp;rs.Close<P>

	&nbsp;&nbsp;&nbsp;' Reopen recordset to insert file<BR>
	&nbsp;&nbsp;&nbsp;rs.Open "MYIMAGES", Connect, 2, 3<P>

	&nbsp;&nbsp;&nbsp;rs.AddNew<BR>
	&nbsp;&nbsp;&nbsp;rs("image_blob") = File.Binary<BR>
	&nbsp;&nbsp;&nbsp;rs("filename") = File.FileName<BR>
	&nbsp;&nbsp;&nbsp;rs("filesize") = File.Size<BR>
	&nbsp;&nbsp;&nbsp;rs("hash") = Hash<BR>
	&nbsp;&nbsp;&nbsp;rs("description") = Upload.Form("DESCR")<BR>
	&nbsp;&nbsp;&nbsp;rs.Update<P>

	&nbsp;&nbsp;&nbsp;Response.Write "File saved."<BR>
	Else<BR>
	&nbsp;&nbsp;&nbsp;Response.Write "File not selected."<BR>
	End If<BR>
	%><BR>

	&lt;/BODY><BR>
	&lt;/HTML><BR>
	</FONT></TD></TR>
	</TABLE>

	<P>
	Click the link below to run this code sample:
	<P>
	<B><A TARGET="_new" HREF="http://localhost/aspupload/04_db/ado.asp">http://localhost/aspupload/04_db/ado.asp</A></B>
	&nbsp;<A HREF="javascript:;" OnClick="open('helppopup.html','','width=400,height=400');"><IMG SRC="help.gif" BORDER="0" ALT="Why is this link not working?"></A>
	<P>

	</BLOCKQUOTE>
	<B><FONT COLOR="#0000A0">ADO-based File Exporting From the Database</FONT></B>
	<BLOCKQUOTE>
	Needless to say, AspUpload offers an ADO-based method for exporting
	files from the database to hard drive. The method
	is called <B>Upload.FromRecordset</B>
	which accepts a recordset value and a local path as parameters.
	The usage of this method is demonstrated by the code sample <B>adoexport.asp</B>.
	<P>
	Click the link below to run this code sample:
	<P>
	<B><A TARGET="_new" HREF="http://localhost/aspupload/04_db/adoexport.asp">http://localhost/aspupload/04_db/adoexport.asp</A></B>
	&nbsp;<A HREF="javascript:;" OnClick="open('helppopup.html','','width=400,height=400');"><IMG SRC="help.gif" BORDER="0" ALT="Why is this link not working?"></A>
	
	</BLOCKQUOTE>
	<B><FONT COLOR="#0000A0">Exporting Files from the Database to a Client Browser</FONT></B>
	<BLOCKQUOTE>
	A file saved in the database can be downloaded to the client browser
	directly without saving it to the hard drive first. No third-party components
	are necessary, downloading from the database can be achieved with
	ADO alone.
	<P>
	To download a file from the database, you need to provide a link
	on your web page pointing to an ASP script that calls Response.BinaryWrite.
	For example, your HTML page contains the following link:
	<P>
	<FONT FACE="Courier New" SIZE=2><B>
	&lt;A HREF="download.asp?id=2">Click here to download&lt;/A>
	</B></FONT>
	<P>
	The file <B>download.asp</B> may look as follows:
	<P>
	<TABLE BORDER=1 CELLSPACING=0 CELLPADDING=3>
	<TR><TD BGCOLOR="#FFFF00">
	<FONT SIZE="1" FACE="Courier New"><B>
	&lt;%<BR>
	Set db = Server.CreateObject("ADODB.Connection")<BR>
	db.Open Connect<BR>
	SQL = "SELECT * FROM MYIMAGES where id = " & Request("id")<BR>
	Set rs =db.Execute( SQL )<BR>
	Response.ContentType = "application/octet-stream"<BR>
	</B>' let the browser know the file name<B><BR>
	Response.AddHeader "Content-Disposition", "attachment;filename=" & Trim(rs("filename"))<BR>
	</B>' let the browser know the file size<B><BR>
	Response.AddHeader "Content-Length", rs("filesize")<BR>
	Response.BinaryWrite rs("image_blob")<BR>
	%>
	</FONT></TD></TR>
	</TABLE>
	<P>
	To display an image stored in the database, you should use an &lt;IMG> tag instead
	of &lt;A>, as follows:
	<P>
	<FONT FACE="Courier New" SIZE=2><B>&lt;IMG SRC="download.asp?id=3"></B></FONT>
	<P>
	Note that the download script must not contain any HTML tags such as &lt;HTML> or &lt;BODY>, just pure ASP script.
	<P>
	The sample files <B>filelist.asp</B> and <B>filelist_download.asp</B>
	demonstrate this technique. The script <B>filelist.asp</B> lists all files previously uploaded to
	the database and generates download links for them. The links invoke the file <B>filelist_download.asp</B>
	similar to the download script shown above.
	<P>
	Click the link below to run this code sample:
	<P>
	<B><A TARGET="_new" HREF="http://localhost/aspupload/04_db/filelist.asp">http://localhost/aspupload/04_db/filelist.asp</A></B>
	&nbsp;<A HREF="javascript:;" OnClick="open('helppopup.html','','width=400,height=400');"><IMG SRC="help.gif" BORDER="0" ALT="Why is this link not working?"></A>
	<P>
	<A HREF="manual_progress.html"><IMG BORDER="0" SRC="next.gif" ALIGN="RIGHT" ALT="Chapter 5: Progress Bar"></A>
	<A HREF="manual_memory.html"><IMG BORDER="0" SRC="previous.gif" ALIGN="RIGHT" ALT="Chapter 3: Uploading to Memory"></A>
	<P>&nbsp;
	</BLOCKQUOTE>
	
	


	</FONT>
	<P>
	<TABLE WIDTH=540 HEIGHT=2 CELLSPACING=0 CELLPADDING=0 BORDER=0>
	<TD BGcolor="#FFCE00"><spacer type=block width=540 height=2></TD>
	</TABLE>
	<P>
	<CENTER>
	<A HREF="index.html"><IMG SRC="logo_small.gif" BORDER=0></A>
	<BR>
	<FONT Face=arial size=1>
	Copyright &copy; 1998 - 2001 <A HREF="http://www.persits.com">Persits Software, Inc.</A><BR>
	All Rights Reserved<BR>
	AspUpload&reg; is a registered trademark of Persits Software, Inc.<BR>
	Questions? Comments? <A HREF="MAILTO:info@aspupload.com">Write us!</A>
	</CENTER>

</TD>
</TABLE>

</BASEFONT>
</BODY>
</HTML>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -