Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Eclipse Scout » Cannot download Excel file created on server onto client's chosen target directory(Dwnloading a binary resource on the server onto the client)
Cannot download Excel file created on server onto client's chosen target directory [message #1860935] Sun, 10 September 2023 11:02 Go to next message
J D is currently offline J DFriend
Messages: 99
Registered: February 2021
Member
Hi there everyone,

I can successfully export table data to an Excel file using the Apache POI library. See the method below:

  private void persistExcelTable(ITable t) {
    FileOutputStream out;
    String fileName;

    try (XSSFWorkbook workbook = new XSSFWorkbook()) {
      if (StringUtility.isNullOrEmpty(invokerName)) {
        fileName = "/home/admin/Desktop/YGApp Export "
            + LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))
            + ".xlsx";
      } else {
        fileName = "/home/admin/Desktop/YGApp " + invokerName + " "
            + LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))
            + ".xlsx";
      }
      out = new FileOutputStream(new File(fileName));

      XSSFSheet sheet = workbook
          .createSheet(TEXTS.get("Sheet") + String.valueOf(workbook.getNumberOfSheets() + 1));

      ..... more code ....

      //
      workbook.write(out);
       // download the file to the client session
      // See
      // https://www.eclipse.org/forums/index.php/m/1440415/?srch=download+excel+file#msg_1440415
      ClientSession.get().getDesktop().openUri(fileName, OpenUriAction.DOWNLOAD);
    } catch (Exception e) {
      throw new VetoException(e.getMessage());
    }
  }


The file is visible on the desktop of the server because that is where it was created but I want users to be able to download the files into a directory of their choice on their devices (computer/tablet/telephone).

While I was looking around on the forum for tips, I came across

https://www.eclipse.org/forums/index.php/m/1440415/?srch=download+excel+file#msg_1440415

and I used

 ClientSession.get().getDesktop().openUri(fileName, OpenUriAction.DOWNLOAD);


to try and download the file to a local client directory, but it doesn't seem to work. Looking at the logs, I see the message

2023-09-10 12:39:43,483 INFO [qtp835227336-42] org.eclipse.scout.rt.ui.html.UiServlet.sendNotFound(UiServlet.java:275) - 404_NOT_FOUND: GET /home/admin/Desktop/YGApp Companies 2023-09-10 12:39:42.xlsx


However, the file is there on the server! What am I doing wrong?

What I would like ideally is after the file is created, a SaveDialog is presented to the user that lets the user chose where the file will be saved and then the file is downloaded there in the same way that a browser downloads files.

Thanks a million for your assistance.

Cheers,

JD

[Updated on: Sun, 10 September 2023 11:10]

Report message to a moderator

Re: Cannot download Excel file created on server onto client's chosen target directory [message #1860936 is a reply to message #1860935] Sun, 10 September 2023 13:52 Go to previous messageGo to next message
Mr Robot is currently offline Mr RobotFriend
Messages: 70
Registered: March 2020
Member
This is based by browser settings. Save as dialog is shown or downloaded to default folder like "Downloads".

Make a BinaryResource file and use it in openUri parameter.

//Create byte[] from excel documents.
public byte[] saveExcelDocument(XSSFWorkbook document) {
		ByteArrayOutputStream out = new ByteArrayOutputStream();
		try {
			document.write(out);
			out.close();
			document.close();

			return out.toByteArray();
		} catch (IOException e) {
			e.printStackTrace();
		}

		return null;
	}

//create BinaryResource object:
byte[] content = saveExcelDocument(excelDocument);
binaryResource = new BinaryResource("myexcelfilename.xlsx", content);

ClientSession.get().getDesktop().openUri(binaryResource, OpenUriAction.DOWNLOAD);
Re: Cannot download Excel file created on server onto client's chosen target directory [message #1860978 is a reply to message #1860936] Tue, 12 September 2023 20:18 Go to previous message
J D is currently offline J DFriend
Messages: 99
Registered: February 2021
Member
Thanks a million, Mr Robot. The file is now downloaded directly into the client's "Downloads" folder, if a default folder was set for downloaded files.

To test the "Save As...." prompt, I removed the setting for a default "Downloads" folder in my browser. The "Save As" dialog then displays correctly
and the client can save the file where he wants to, within the limits of the file system permissions.

Cheers,

JD
Previous Topic:Application not starting because of config.properties invalid private/public key pair
Next Topic:Eclipse Scout 23.2 is out now!
Goto Forum:
  


Current Time: Mon Apr 22 19:45:09 GMT 2024

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

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

Back to the top