Struts: Upload a file with progress bar - java

i want to upload files with Struts using the org.apache.struts.upload.FormFile but i would like to put a progress bar (like the gmail uploads forms or some like that), to upload the file (the files are big like 100Mb so the user must see what's going with the application). How i can do it?
Regards :)

You'll basically need to bring some shot of Ajax in so that the webbrowser and webserver can comminicate with each other asynchronously without the need to refresh/resubmit the page again and again. With Ajax the client will be able to poll the server for current progress at intervals and update the progress bar in the page accordingly.
Long story short, here's a nice blog article which covers this for the legacy Struts framework: http://kencochrane.blogspot.com/2006/03/ajax-struts-file-upload-progress-meter.html

Related

Dialog box in Spring MVC controller

Is there a way to display a (preferably modal) dialog box in an servlet controller ? If it isn't modal that is something I can deal with (!?) as long as it initially appears above the browser.
Essentially I have a form with a table and 2 buttons on it. One button takes the user to a different place in the workflow and is irrelevant to this question (just to explain why the other one doesn't 'go anywhere').
The other button currently goes back to an MVC controller, calls some code to export the table to excel and then reloads the web page. This is all working okay except the way I am calling the dialog box is calling it underneath the browser. I suspect this is because I am sending null as the frame but I'm not sure what to put in its place ?
JOptionPane.showMessageDialog(null, "Export Completed.", "Excel Export", JOptionPane.INFORMATION_MESSAGE);
Many thanks.
I am not too sure what you are trying to achieve from your question and without any code examples. However, presumably what you are doing (guessing here), is that you are trying to export some data from some data source and convert it to an Excel file. You have to keep 3 things in mind.
Web applications work via HTTP requests and responses. So the only thing a Servlet can do is send back an HTTP response that indicates that the export was successful. Whether you use a traditional page, or maybe use AJAX to avoid refreshing the page is purely your design choice. You could also start with a simple page and then change to AJAX combined with JQuery later once you get used to what is happening.
Exporting the excel sheet to the server does not mean that your client has access to it. Speculating here, but you will probably need a mechanism for your user to get the file. One simple approach used by many webapps is to actually send the Excel file as the Servlet response itself. So what would happen is that when the export is completed the browser starts receiving the file and the user sees it downloading. From your servlet you will just need to set the right mime-type and set the content-disposition header to state that the file is an attachment (so that the browser downloads it as a file).
httpresp.setContentType("text/csv");
httpresp.setHeader("Content-disposition", "attachment; filename=\"export.csv\"");
You will probably also need to set the file size. There are various full examples on SO if you look for further details.
When performing these operations remember that users can interrupt the browser or refresh. If the user presses F5 he might cause your application to do the export again. One common approach to this is called 'redirect-after-post'. Basically you redirect the user to a page which just displays the outcome, without performing the operation again. This way if he presses refresh, he is just refreshing the page with the message.

Downloading generated excel file using ajax

I am developing an application using struts2 framework. When user provides date range, I need to prepare the reports excel file and show that in download pop-up. I am able to create io stream of excel, but I am not getting how can I open that pop-up using ajax.
I am generating file on the fly and returns the stream. Therefore not able to give file path. I want ajax call to show some message like "Please wait..". Because file generation takes more time and it looks like link is not working..
There's nothing Ajax about downloading a file.
Your request can be an ajax request, but simply give the generated file path and the browser will do the downloading.
No need to perform any AJAX calls.
You just need to return the byte[] as a Struts2 Stream result,
configured with a ContentDisposition: attachment; (and not ContentDisposition: inline, that will try to open it inside the browser instead of asking about downloading or opening with a desktop application).
To notify the user that something is going on, that the request is sent and the system is not frozen, you need an Loading OVERLAY.
An Overlay is an element that is placed over your page, generally with a partially transparent background, an animated image saying "Loading..." , and a modal behavior (it won't close until the page is changed, and it will prevent double post of the same request, like double clicks etc...).
Of course if you open an attachment, the page will not change, then you'll need to intercept the end of the downloading and close the overlay by yourself, or give the user a button to close it.
To intercept it, you can try the Struts2 Execute and Wait Interceptor.
If you want to create a custom overlay, you can generate your Loading images with the ajaxload.info Generator.
If you instead don't want to reinvent the wheel, you can take a look at existing overlays, like those from jQuery TOOLS.
On the success method of ajax do not return the stream, instead save the file on server and open a pop up with address to an action that returns correct file. But pop ups might be blocked in some browsers so you need to allow that for your site (local host or other URL)

Progress info on client side?

I've a Servlet which is used to generate and download file using ServletOutputStream. It's working well. But the Servlet some time take several seconds to process request depending on size of the file.
My questions is I want show the some progress information to client side. How do I do it?
This comes with any good Javascript toolkit, like jquery for instance. They have a progress bar.
http://jqueryui.com/
http://jqueryui.com/progressbar/

Upload files in JSP/Servlet

i know how upload files in jsp/servlet, i already use fileupload from apache, but i don't know how to return some status of the upload to the user, for example, actually when someone select the file to upload, they have to click in 'upload' button and just when the upload finish the jsp page redirect to the another page.
I want they can see a percentage of how much will take to complete the upload operation.
i already implement a listener using fileupload, but i don't know how to show this information to the user, 'cause just when the upload is finished that i can redirect to another jsp page.
Any ideas ?
Best regards,
Valter Henrique.
That's not possible in combination with plain HTML <form> with <input type="file">. To the point, you've got to send multiple simultaneous requests. A form submit with file upload will namely block as long as the upload is in progress.
Your best bet is using JavaScript/Ajax and/or Flash. There are several free file upload components supporting a progress bar, like Uploadify and SwfUpload.
If your servlet is well written, you should be able to use it unchanged.

Upload progress bar Java Servlet?

I want to show upload progress bar for my uploads using servlet. I tried Ajax, iFrame technique. The page is not reloading and the file also getting uplaoded. But, progress bar is not coming. Is there any jQuery progress plugin available for java servelts?
Thanks!!
I strongly recommend jQuery Uploadify plugin for ajax file uploads. It comes with a progress bar as well. You can find an example on their demo page.
Integration with JSP/Servlet isn't that hard. You can basically keep the servlet code for "regular" file uploads unchanged. I've however ever written a mini tutorial in an answer here (check the "update" part of the answer), you may find it useful as well.

Categories

Resources