One of the great advantages to this new version of
This sample show how to maintain the process of uploading to a database. If any action does nottake place, all actions will go back to their original state. This example uses the upload.mdb that is in the Samples directory.
First let's look at the form section for the first page. With this form you submit two text boxes and a file. The file will be exported into the database.
..... <FORM ENCTYPE="MULTIPART/FORM-DATA" METHOD="POST" ACTION="TransDBFormResp.asp"> <TABLE WIDTH="100%"> <TR> <TD ALIGN="RIGHT" VALIGN="TOP">Author</TD> <TD><INPUT TYPE="TEXT" NAME="AUTHOR"></TD></TR> <TR> <TD ALIGN="RIGHT" VALIGN="TOP">Description</TD> <TD><INPUT TYPE="TEXT" NAME="DESCRIP"></TD></TR> <TR> <TD ALIGN="RIGHT" VALIGN="TOP">Enter Filename:</TD> <% '--- '--- Note the use of the TYPE="FILE" specification '--- %> <TD ALIGN="LEFT"><INPUT TYPE="FILE" NAME="FILE1"><BR> <B><I><SMALL>Note: if a button labeled "Browse..." does not appear, then your browser does not support File Upload. For Internet Explorer 3.02 users, a free add-on is available from Microsoft. Please see the SA-FileUp documentation for more information.</SMALL></I></B> </TD> </TR> <TR> <TD ALIGN="RIGHT"> </TD> <TD ALIGN="LEFT"><INPUT TYPE="SUBMIT" NAME="SUB1" VALUE="Submit"></TD> </TR> </TABLE> </FORM> ....
Next let's look at the response page. It contains the instance of
On this page, you indicate that you will be using MTS by including the following at the top of the page:
<%@ LANGUAGE="VBSCRIPT" TRANSACTION = Required %>
Throughout the script, you can apply error handle with "ObjectContext.SetAbort", to control transaction.
Here is the code of the first major section:
....
<%
'---
'--- Create an instance of SA-FileUp
'---
Set upl = Server.CreateObject("SoftArtisans.FileUp")
'---
'--- Set the default path to store uploaded files. This step is not
'--- strictly necessary, but performance is better if you specify it.
'---
upl.Path = "c:\temp"
%>
<% If Not IsObject(upl.Form("FILE1")) Then %>
The form contains a tag named "FILE1" which is not of TYPE="FILE". Please
check your form definition.
<% elseif upl.Form("FILE1").IsEmpty = 1 Then
'---Abort of upload if file is empty
ObjectContext.SetAbort
%>
<%
'---
'--- Even if the filename field is empty, all of the other form elements are
'--- available.
'---
%>
In this section, we:
In the last part, we call SetAbort to abort the transaction, since the file element is blank.
Further down the page we implement another condition:
....
<% ElseIf upl.Form("FILE1").ContentDisposition ≪> "form-data" Then
'---Abort upload if there ContentDisposition is incorrect
ObjectContext.SetAbort
%>
Your upload did not succeed, most likely because your browser
does not support Upload via this mechanism.
....
This condition tests the form to see if it supports the protocol for file transfer that
The next section deals with another condition to verify whether one or both of the textboxes are blank. If so, the transaction is aborted:
....
<%Else
if upl.form("AUTHOR") = "" or upl.form("DESCRIP") = "" then
'---Abort upload if there ContentDisposition is incorrect
ObjectContext.SetAbort
End IF
%>
....
We then create an ADO recordset and establish error handling of the database interaction:
.... <% On Error Resume Next set rsBlob = Server.CreateObject("adodb.recordset") 'Open Recorsets rsBlob.Open "IntermediateBlobTable", "AccessUpload", 2, 3 '--- '--- For SQL Server Users, use the following line to open the recordset '--- Make sure you replace: '--- <myserver> by your SQL Server name '--- '--- rsBlob.Open "IntermediateBlobTable", "driver={SQL Server};server=<myserver>;uid=sa;pwd=;database=SQLServerUpload", 2, 3 'Add new record and update rsBlob.AddNew rsBlob.Fields("author") = upl.form("AUTHOR") rsBlob.Fields("descrip") = upl.form("DESCRIP") upl.form("FILE1").SaveAsBlob rsBlob.Fields("filecol") rsBlob.Update rsBlob.Close Set rsBlob = Nothing if Err <> 0 Then '---Abort upload if there are any errors ObjectContext.SetAbort %> .... <% End If End If %>If any errors occur due to the database interaction, the entire process is rolled back. Otherwise, the transfer takes place.
At the very end of this page, we can display a message or perform an action based on the events of the transaction:
....
<%
'--- Message appended to page if upload is a failure
Sub OnTransactionAbort
Response.Write("<BR><hr><font color='red'><b>Transaction has been aborted.</b></font>")
End Sub%>
<%
'--- Message appended to page if upload is successful
Sub OnTransactionCommit
Response.Write("<BR><hr><font color='Green'><HR>Transaction has been committed.</b></font>")
End Sub
%>
....
As you can see, adding transactional properties to your upload is easy.
This completes the Transactional Processing tutorial. You can also try the Common Client-Side Solutions tutorial.
| Previous Page | Next Page |