How to use the file attachment control on a form.
You can run this technique code from:
NOTE First make sure that database is running on your localhost SilverStream Server | |
See the chapter on the Form Designer in the Tools Guide |
The example lets users upload documents and images into a database. It adds a file to the database as a new record, downloads content from the database as a file, and updates the database with file contents the user has changed.
The following code shows how to display a dialog to obtain the file to upload and the file's path. The code creates a new record in the database for this file, uploads it, and (if requested by the user) deletes the local file.
private void handle_btnAttach_actionPerformed(ActionEvent evt) { File fileSelected; try { fileSelected = attachFile.chooseFile( "Please select a file to upload", null, false); if (fileSelected != null) { if (!filename.isEnabled()) { filename.setEnabled(true); fldFileDescription.setEnabled(true); } agData.gotoLast(); agData.insertAfter(); filename.setText(fileSelected.getName()); // Save the entire path name lblFilePath.setText(fileSelected.getPath()); agData.updateRows(); if (chkDelete.getState() ) attachFile.upload(fileSelected, true); else attachFile.upload(fileSelected, false); } } catch (Exception e) { agDialog.displayError(e); } }
attachFile
is the name of an AgcFileAttachment control on the form.
chooseFile()
displays a dialog to get the name and path of the file to upload into the database.
gotoLast()
moves to the last record in the buffer.
insertAfter()
inserts a new row after the current row, which is the last row.
filename.setText()
sets the File Name text field to the user-specified name obtained from the dialog. (filename has previously been bound to a field in the database.)
lblFilePath.setText()
sets the invisible label to the file path that is also obtained from the dialog. This path is used for uploading the file contents into the database.
updateRows()
updates the database rows. This sets the file name and file description. When creating a new record in the database, always call updateRows() before calling upload()
.
upload()
to true to delete the local file after upload; set it to false otherwise.
The following code downloads field contents from the database. It creates a user-specified file from the content and runs it using the appropriate executable.
private void handle_btnEdit_actionPerformed(ActionEvent evt) { File fileDownload = new File(filename.getText()); try { attachFile.download(fileDownload, true); lblFilePath.setText(fileDownload.getPath()); m_bFileChanged = true; } catch (Exception e) { e.printStackTrace(); agDialog.displayError("Error downloading file", e); } }
download()
automatically finds the appropriate executable (such as MSWord or MSExcel) to run the file.
m_bFileChanged
to true to specify that the file in the local file system has been changed, that is, a download has occurred. This is necessary for future save attempts. The code for saving a modified file relies on the bgFileChanged
value to indicate whether the file was previously downloaded from the database. If the file was previously downloaded, its content can be saved to the database.
The following code saves the content of a file to the database. The user must have previously downloaded the content of this file from the database.
public boolean saveFileAttachment() { if (m_bFileChanged) { File fileSelected = new File(lblFilePath.getText()); try { if (chkDelete.getState() ) attachFile.upload(fileSelected, true); else attachFile.upload(fileSelected, false); m_bFileChanged = false; return true; } catch (Exception e) { agDialog.displayError(e); return false; } } else { agDialog.showMessage("File Upload Error", "No file has been changed. There is nothing to update."); return false; } }
m_bFileChanged
variable is set to true when the user clicks the Edit File button.
File
object using the text from the invisible label. This text indicates the file name and file path, and was set during the download. (The path might be the default path, that is, where SilverStream is running. This is the case if the user did not specify a path during the download.)
upload()
is set to true, and the method deletes the local file after the upload; otherwise, the method does not delete the local file after the upload. By using the file name and path from the invisible label, upload()
saves the file that is in the location where the previous download occurred. Even if the user types in a different file and path, this method will upload the correct one.
m_bFileChanged
parameter to false. This prevents all save attempts of files that have not been preceded by a download. That is, the user can save a file only after it has downloaded it from the database.