Application Techniques



Using File Attachments on Forms

How to use the file attachment control on a form.

About this technique

Details

Category

Java Client Techniques> General techniques for controls

Description

You'll learn about:

You can run this technique code from:

NOTE   First make sure that database is running on your localhost SilverStream Server

Related reading

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.

Uploading a new file into the database   Top of page

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); 
  } 
  } 

Notes about the code

Downloading content from the database as a file   Top of page

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); 
  } 
  } 

Notes about the code

Saving modified content to the database   Top of page

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; 
  } 
  } 

Notes about the code






Copyright © 2000, SilverStream Software, Inc. All rights reserved.