How to export excel file in GWT? - java

My requirement is to export an excel from GWT application on click of export button. In my case There is no need to send data from client to server as I can directly obtain data from database(same data is being populated on the client that is being exported).
All I tried to send a RPC call from Client site . but I am not able to figure out how to export excel on receiving the RPC call at server site.

This is certainly possilbe 100% client side.
You can use an Excel JavaScript builder like https://github.com/stephenliberty/excel-builder.js and call it from GWT with jsni or jsinterop.
Later you can save the file by
For Internet explorer for example filesaver.js https://github.com/eligrey/FileSaver.js/ in the same way.
For other browsers using HTML5 downloadv (using FileDownloadBuilder.createFileDownload().generateTextDownloadLink()) from https://github.com/akjava/html5gwt
Note for the generating an Excem file you could also just create an html file with table tags and save it as .xls, it with filesaver.js.
I use the html solution and filesaver.js/html5 download in my project and this works without a problem, and without the need for any serverside code, or any plugin on the client. 100% HTML5/javascript.

There is no way generating a excel file on the client site with native GWT without adding a third party product. (see knarf answer below)
I personally prefer this solution:
open an new window on the client
use a url, that triggers a servlet on the server
let the server generate the excel file
save the newly generated file inside the webspace
return the url to the excel file to the client
This works for me.

Related

java - send a file to client by file URL without download on the server

In my web application I have a link which, when clicked, invokes an external web service to retrieve a download URL for a file.
I need to send back to client the file which is beyond this URL, instead of the download URL retrieved from the web service. If possible, I would also like to do it without having to download the file on my server beforehand.
I've found this question about a similar task, but which used PHP with the readfile() function.
Is there a similar way to do this in Java 8?
If you doesn't even want to handle that file you should answer the request with a redirect (eg HTTP 301 or 302). If you want to handle the file you should read the file in a byte buffer and send it to the client which would make the transfer slower.
Without seeing your implementation so far, this is my best suggest.

reading logs and displaying them on a web browser using Java

I have logs in folder /home/a/b (located in a remote server)
I want to display logs in my web browser using Java. To get data in the folder /home/a/b and to display them on my web page, what are some methods(API) I can use?
The simplest approach I can imagine is using a SSH API like JSch,
but I don't know how performatic this is.
Anyway, here goes a good example:
https://stackoverflow.com/a/9019095/7528396
Note that you can read the remote file and render line by line.

How to save/open files on server with GWT

I have a BIRT report that I've created in GWT and I'd like to save it to the server and then open it back up. I feel like hardcoding the url is the wrong route to go (ie C:/files/foo/foo.html) but I'm not having much luck figuring out another way of doing it. I tried using GWT.getModuleBaseForStaticFiles() but if i use that I get an unsatisfied link error in BIRT. What can I use to save/open a file, what is considered the best practice? Thanks.
edit: bad wording, the BIRT report is generated server-side. I'd like to be able to save it server side and then open it up in a new browser window (using window.open I'd imagine?). Nothing is uploaded from the client side.
If your BIRT report is a static file (Not generated during the runtime of your application) You can create a simple Servlet that read the file and copy the data to the OutputStream of the response. If it's generated at the runtime, the client will request it to the server, the server will generate it and return the URL location of the report. The client can open the report with that URL.
Example:
If your BIRT is located under foo/foo.bar you can map a servlet as /foo/*. After mapping it the user will request the url www.yourselver.com/foo/foo.bar The servlet at the doGet method can read the file and stream the content back to the client.

How do I use a GWT RPC to save a file on the client side?

I'm looking for specific code on how to send a file from the server-side of a GWT application so that the client-side user can save it on his machine. Currently, the app allows the user to upload a file, reads it, and puts certain values from the file into editable text boxes. When the user hits the "save as" button, it collects that edited data, puts it back into the file string, and sends that string to the server, where I want it to be put into a file and pushed back to the user on the client side, so that they can save it to their machine. How exactly do I accomplish that?
Sorry if this seems like an obvious thing, but I'm relatively new to GWT and java in general. Thanks!
I think that you want the way for download a file using content-type from server using GWT.
The easiest way that I have found is create a iFrame :
import com.google.gwt.user.client.ui.NamedFramerdddccvc
...
NamedFrame iframe = new NamedFrame(frameName);
iframe.setVisible(false);
parent.addChild(iframe);
iframe.setUrl(url);
It's important that the url from the server return a page with content type "text/plain" or using the valid requested.
What you can do is create a servlet, that generates the text as content and set the matching mimetype for the content. In you app you can call this servlet via the by Fernando suggested IFrame method.
There are many suggestions here on Stackoverflow on how to do it. Search for [java] file download serlvet and you will find lots of examples/guidelines on how to do this.

java - checking file content before uploading to server

i have got a situation now.
I need to develop a webpage where user can select a file to upload and before uploading the file to server i need to check first few lines of the file whether the data is valid or not and if the data is valid then upload the file, if not through an error message.
the file will be text file.
thanks,
Sandeep
HTML/Javascript does not offer a way of reading the contents of a local file. You must either upload it and check it in the server.
If you really want a client side check, you then must build a signed applet(or even ActiveX) to run in your webpage and handle the upload instead of using plain HTML.
You should perform your validation on the server side, right before you perform the upload.

Categories

Resources