Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Eclipse Scout » File download from server
File download from server [message #892548] Thu, 28 June 2012 15:13 Go to next message
Stefan Mutschler is currently offline Stefan MutschlerFriend
Messages: 26
Registered: June 2012
Location: Baden-Baden, Germany
Junior Member
Can someone please give me a short sample how to download a server generated file in a client independant way? The scenario is to create a file (txt, pdf, whatever) on the server side and to save it to the server file system. Afterwards I would like to offer the user e.g. a button or linkbutton to request the generated file and save it to the local file system of the client machine.

I found the forum message on using the RemoteFileService which is working perfectly to read the content of the file and display it in the client window. But are there also methods that I can use to save the file on the client side independant from the client implementation I am working with?

Thanks a lot in advance

Stefan




Re: File download from server [message #892554 is a reply to message #892548] Thu, 28 June 2012 15:39 Go to previous messageGo to next message
Ivan Motsch is currently offline Ivan MotschFriend
Messages: 154
Registered: March 2010
Senior Member
There are two things to help:
1) The scout IFileChooser stores a file on the server side (in web ui) or on the local filesytem (rich client)

2) IDesktop.openBrowserWindow is used in web ui to publish a file to the user (download)

Typically both parts are used in an application:

Solution suggestion:
create an utility function in your project to download a (server) file on client side say DownloadUtility.java:

  /**
   * Used in web ui to force a file download for the user.
   * <p>
   * This is normally used on {@link IFileChooserField}s with {@link IFileChooserField#isTypeLoad()}=false.
   * <p>
   * In web ui the file chooser in save mode creates a temp file. The temp file is written by bsi crm logic and finally
   * this utility method downloads the temp file to the end user.
   */
  public static void downloadFile(File tempFile) {
    if (!UserAgentUtility.isWebClient()) {
      return;
    }
    IDesktop desktop = ClientJob.getCurrentSession().getDesktop();
    if (!desktop.isGuiAvailable()) {
      return;
    }
    desktop.openBrowserWindow(tempFile.getAbsolutePath());
  }


In your form or menu or whatever create the file (on the server) and push it to the user

generated file f....
IOUtility.writeContent(f.getAbsolutePath(), s);
DownloadUtility.downloadFile(f);


I also can give you a comoplete "Save As..." menu which of course can also placed as a button to the form since it is an IAction.

Client code:
@Order(10.0)
          public class SaveAsMenu extends AbstractMenu {

            @Override
            protected boolean getConfiguredSingleSelectionAction() {
              return true;
            }

            @Override
            protected String getConfiguredText() {
              return TEXTS.get("SaveDocumentAs");
            }

            @Override
            protected void execAction() throws ProcessingException {
              String filename = getNameColumn().getSelectedValue();
              File outFile;
              if (UserAgentUtility.isWebClient()) {
                //web ui will show the browser file chooser later-on to store the file locally
                outFile = new File(IOUtility.createTempDirectory("download"), filename);
              }
              else {
                FileChooser chooser = new FileChooser();
                chooser.setTypeLoad(false);
                chooser.setFileName(filename);
                File[] files = chooser.startChooser();
                outFile = (files != null && files.length > 0 ? files[0] : null);
              }
              if (outFile != null) {
                IMyDocumentService svc = SERVICES.getService(IMyDocumentService.class);
                RemoteFile rf = svc.downloadTemplate(filename);
                FileOutputStream o;
                try {
                  o = new FileOutputStream(outFile);
                  rf.writeData(o);
                  //will do nothing when in rich client mode, but show a web save as dialog in web ui
                  DownloadUtility.downloadFile(outFile);
                }
                catch (FileNotFoundException e) {
                  throw new ProcessingException("unable to find file", e);
                }
                catch (IOException io) {
                  throw new ProcessingException("unable to write file", io);
                }
              }
            }
          }
Re: File download from server [message #892642 is a reply to message #892554] Thu, 28 June 2012 22:41 Go to previous messageGo to next message
Stefan Mutschler is currently offline Stefan MutschlerFriend
Messages: 26
Registered: June 2012
Location: Baden-Baden, Germany
Junior Member
Works! Thank you very much. EXACTLY what I was searching for.

Is there a package convention for such classes? I have created an xx.yyy.client.util package for DownloadUtility.java. Any recommendations?
Re: File download from server [message #892664 is a reply to message #892642] Fri, 29 June 2012 04:56 Go to previous messageGo to next message
Jeremie Bresson is currently offline Jeremie BressonFriend
Messages: 124
Registered: November 2010
Senior Member
We have a lot of post related to this

* Wiki: How to use RemoteFileService
* Forum: This post
* Forum: RemoteFileService (RemoteFileService with large directories)
* Forum: File handling

Maybe we need to merge them all together...
Re: File download from server [message #892680] Fri, 29 June 2012 07:41 Go to previous messageGo to next message
Stefan Mutschler is currently offline Stefan MutschlerFriend
Messages: 26
Registered: June 2012
Location: Baden-Baden, Germany
Junior Member
Yes, I know these posts, thanks. I think my question was a little bit confusing (and maybe rather content for another post?):

My question was targeting conventions. A scout project seems well structured to me and I just wanted to know if you have best practices / conventions where to store e.g. self written utility classes or other stuff not related directly to the standard scout project layout.

Maybe not really important, I'm just curious about your experiences...
Re: File download from server [message #892733 is a reply to message #892680] Fri, 29 June 2012 11:58 Go to previous messageGo to next message
Stephan Leicht Vogt is currently offline Stephan Leicht VogtFriend
Messages: 104
Registered: July 2015
Senior Member
Hi Stefan

In our application we store utility classes in packages like these:

xx.yy.client.common e.g. ZzClientUtility for utility methods only used in the client.
xx.yy.shared.common e.g. ZzSharedUtility for utility methods used in the client and server.
xx.yy.server.common e.g. ZzServerUtility for utility methods only used in the server.

Was that the info you where searching for?

Greetings
Stephan
Re: File download from server [message #892795 is a reply to message #892733] Fri, 29 June 2012 17:53 Go to previous message
Stefan Mutschler is currently offline Stefan MutschlerFriend
Messages: 26
Registered: June 2012
Location: Baden-Baden, Germany
Junior Member
Yes, thank you. Now after you having replied I recognized that the SDK is also using 'common' as package naming e.g. 'xx.yy.server.services.common.sql'. I like naming conventions since they speed up development and help keeping the structure tidy.

Thanks for your time.
Previous Topic:Scout Toolbar
Next Topic:Smart field with master
Goto Forum:
  


Current Time: Fri Apr 19 19:54:20 GMT 2024

Powered by FUDForum. Page generated in 0.02826 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top