|
|
Line 1: |
Line 1: |
− | {{ScoutPage|cat=HowTo 4.0}}
| + | The Scout documentation has been moved to https://eclipsescout.github.io/. |
− | | + | |
− | 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 handler, no permissions).
| + | |
− | # Add a new file chooser field to the main box (name: FileField).
| + | |
− | # In Scout Object Properties view, choose ''Type Load'' and ''Mandatory'' in ''Behavior'' for the newly created field.
| + | |
− | # Add a new variable named RemoteFile (Bean type RemoteFile) to the form.
| + | |
− | | + | |
− | = Add File Upload Code =
| + | |
− | Overwrite execStore of the NewHandler as follows:
| + | |
− | <source lang="java">
| + | |
− | 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);
| + | |
− | }
| + | |
− | | + | |
− | IUploadFileService service = SERVICES.getService(IUploadFileService.class);
| + | |
− | UploadFileFormData formData = new UploadFileFormData();
| + | |
− | exportFormData(formData);
| + | |
− | formData = service.create(formData);
| + | |
− | </source>
| + | |
− | | + | |
− | = Add Menu to Open Form =
| + | |
− | # Add a new sub menu to the Desktop / FileMenu (name UploadFileMenu).
| + | |
− | # Overwrite execAction of the new menu as follows:
| + | |
− | <source lang="java">
| + | |
− | UploadFileForm form = new UploadFileForm();
| + | |
− | form.startNew();
| + | |
− | form.waitFor();
| + | |
− | </source>
| + | |
− | | + | |
− | = Server Side Handling =
| + | |
− | Use the following code in the create method of the UploadFileService:
| + | |
− | <source lang="java">
| + | |
− | 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;
| + | |
− | </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.
| + | |