|
||
| Inside Technique : Sokoban Level Designer : Client Data Access with HTML Applications : FileOpen function The first function we will be covering is the FileOpen function. Since this is a fairly large block of code, we will be breaking it into 3 steps, followed by an explanation of these steps. Note that this function uses JScript version 5 constructs, and thus will only work in IE5. Step 1 : Open the Common Dialog
function FileOpen(){
//Decide which directory in which to open CommDlg
DP=TDC1.dataURL;
if(DP=='')
DataPath=location.pathname.substr(1)
else
DataPath=DP.substring(0,DP.lastIndexOf('\\')
//Initialize CommDlg properties
CommonDialog1.DialogTitle="Select Data File";
CommonDialog1.Filter=
"Data File (*.dat)|*.dat|All Files (*.*)|*.*";
CommonDialog1.InitDir=DataPath;
//Open CommDlg
try{
CommonDialog1.ShowOpen();
}
//An Error has occured
catch (exception){
//Strip high word from error number
errnum=exception.number & 0xFFFF;
//Check if user has pressed Cancel
if(errnum==32755)
return
//Some other error, display Error Message
//and return
else{
alert('ERROR:\n\n'+exception.description);
return;
}
}
...continued...
The first thing this function does is decide in which directory to open the Common Dialog. If the user has previously opened a data file, the Common Dialog should open in the same directory as that data file. This code gets this directory by obtaining the DataURL property of the TDC, and stripping the filename from it. If, on the other hand, the user has not previously opened a data file, the Common Dialog should open in the same directory from which the user has loaded the page. Following this, the function sets the Common Dialog's properties appropriately and proceeds to call the Open File dialog. This call is enclosed within a JScript try...catch error handler. The handler first strips off the high word of the Error number (This is because of a bug in the JScript engine...Microsoft has since fixed it, I think...still better safe than sorry). Then the handler checks if the error is a CdlCancel (covered in the first article of this series), and quietly returns if so. If some other error has occured, the handler displays the error message and returns. Step 2 : Open the ADO Connection
//Set DataFile and DataFilePath
DataFile=CommonDialog1.FileTitle;
if(DataFile=="")
return;
DataFilePath=CommonDialog1.FileName;
//Set DSN-less Connection String
DSNStr=
"DRIVER={Microsoft Text Driver (*.txt; *.csv)}; DBQ="+
DataFilePath.substring(0,DataFilePath.lastIndexOf('\\'));
//If Previous Connection is Open, Close it
if(ADOConn.State==1)
ADOConn.Close();
//Open ADO Connection
try{
ADOConn.Open(DSNStr);
}
catch(exception){
errnum=exception.number & 0xFFFF;
//ODBC Error - Path Not Found
if(errnum==16389){
alert('The path you have specified does not exist');
return;
}
else{
//Some other error
alert('ERROR:\n\n'+exception.description);
return;
}
}
...continued...
The piece of code above first sets the DataFile variable to hold the name of the file, and the DataFilePath variable to hold the full path of the file that the user has selected. Both DataFile and DataFilePath are global variables, and will be used by the other functions in this library. The code then tests if the connection object is open by examining it's State property. If the value of State is 1 (Open), the connection object is closed. ADO Connection objects need to be closed before re-opening them with a new connection string. Finally, the connection object is opened using a DSN-less connection string with the DBQ pointing to the directory in which the file that the user selected resides. The error handling code checks for a Path Not Found error, for which it displays a custom error message. If some other error has occured, it displays the default message for that error. Step 3 : Open the file in the TDC
//Point the TDC to the Data File
TDC1.dataURL=DataFilePath;
try{
TDC1.Reset();
}
catch(exception){
errnum=exception.number & 0xFFFF;
//Trap 'File Not Found' error
if(errnum==65531){
alert('Data File does not exist');
return;
}
else{
alert('ERROR:\n\n'+exception.description);
return;
}
}
}//end function FileOpen()
The last task of the function is to open the data file in the TDC. It does this by setting the dataURL of the TDC to the full path of the selected file, previously stored in the DataFilePath variable, and then resetting the TDC. The error handler for the TDC Reset displays a custom error message if it detects the 'File Not Found' error, or the default error message if some other error has occured. That's it for the FileOpen function. In the next section, we will look at the FileNew function. Page 1:Sokoban Level Designer : Client Data Access with HTML Applications © 1997-2000 InsideDHTML.com, LLC. All rights reserved. |