Skip to main content

Notice: this Wiki will be going read only early in 2024 and edits will no longer be possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

Difference between revisions of "Scout/HowTo/3.8/Transfer a file from the client to the server"

< Scout‎ | HowTo‎ | 3.8
(New page: This tutorial describes how to transfer a file from the client to the server by using RemoteFile. = Create the Form = # Create a new form (name: UploadFileForm, no form id, no modify hand...)
 
(Server Side Handling)
 
(2 intermediate revisions by the same user not shown)
Line 32: Line 32:
 
= Add Menu to Open Form =
 
= Add Menu to Open Form =
 
# Add a new sub menu to the Desktop / FileMenu (name UploadFileMenu).
 
# Add a new sub menu to the Desktop / FileMenu (name UploadFileMenu).
# Overwrite execStore of the new menu as follows:
+
# Overwrite execInitAction() of the new menu as follows:
 
<source lang="java">
 
<source lang="java">
 
UploadFileForm form = new UploadFileForm();
 
UploadFileForm form = new UploadFileForm();
Line 72: Line 72:
 
}
 
}
 
return formData;
 
return formData;
 +
</source>
 +
 +
Alternatively you can store the RemoteFile on the server disk:
 +
<source lang="java">
 +
RemoteFile remote = formData.getRemoteFile();
 +
SERVICES.getService(IRemoteFileService.class).putRemoteFile(remote);
 
</source>
 
</source>
  
 
Now you are able to open the new form via the menu, choose a file to upload, and handle the file content on server side.
 
Now you are able to open the new form via the menu, choose a file to upload, and handle the file content on server side.
 +
 +
= References =
 +
[http://www.eclipse.org/forums/index.php/t/497216/ Forum post 497216]

Latest revision as of 08:29, 1 October 2013

This tutorial describes how to transfer a file from the client to the server by using RemoteFile.

Create the Form

  1. Create a new form (name: UploadFileForm, no form id, no modify handler, no permissions).
  2. Add a new file chooser field to the main box (name: FileField).
  3. In Scout Object Properties view, choose Type Load and Mandatory in Behavior for the newly created field.
  4. Add a new variable named RemoteFile (Bean type RemoteFile) to the form.

Add File Upload Code

Overwrite execStore of the NewHandler as follows:

File file = getFileField().getValueAsFile();
String name = IOUtility.getFileName(file.getAbsolutePath());
String fileExtension = IOUtility.getFileExtension(name);
try {
  RemoteFile remoteFile = new RemoteFile(name, System.currentTimeMillis());
  remoteFile.readData(new FileInputStream(file));
  remoteFile.setContentLength((int) file.length());
  remoteFile.setContentTypeByExtension(fileExtension);
  setRemoteFile(remoteFile);
}
catch (IOException e) {
  throw new ProcessingException("Could not remote file", e);
}
 
IUploadFileProcessService service = SERVICES.getService(IUploadFileProcessService.class);
UploadFileFormData formData = new UploadFileFormData();
exportFormData(formData);
formData = service.create(formData);

Add Menu to Open Form

  1. Add a new sub menu to the Desktop / FileMenu (name UploadFileMenu).
  2. Overwrite execInitAction() of the new menu as follows:
UploadFileForm form = new UploadFileForm();
form.startNew();
form.waitFor();

Server Side Handling

Use the following code in the create method of the UploadFileProcessService:

try {
  // get content from remote file
  ByteArrayOutputStream bos = new ByteArrayOutputStream();
  formData.getRemoteFile().writeData(bos);
  bos.flush();
  byte[] content = bos.toByteArray();
 
  int updated = SQL.update("" +
      " UPDATE FILES " +
      "    SET CONTENT = :content " +
      "  WHERE FILENAME = :filename "
      , new NVPair("filename", formData.getRemoteFile().getName())
      , new NVPair("content", content)
      );
 
  if (updated == 0) {
    SQL.insert("" +
        " INSERT INTO FILES " +
        "    (FILENAME, CONTENT) " +
        "   VALUES " +
        "    (:filename, :content) "
        , new NVPair("filename", formData.getRemoteFile().getName())
        , new NVPair("content", content)
        );
  }
}
catch (IOException e) {
  throw new ProcessingException("Could not persist remote file.", e);
}
return formData;

Alternatively you can store the RemoteFile on the server disk:

RemoteFile remote = formData.getRemoteFile();
SERVICES.getService(IRemoteFileService.class).putRemoteFile(remote);

Now you are able to open the new form via the menu, choose a file to upload, and handle the file content on server side.

References

Forum post 497216

Back to the top