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)
Related
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.
In my jsp someone wants to export a query result to csv. Can't use frameworks. At the moment, here is the process:
press button, go to servlet
prepare data for csv
make csv and save to server
back to jsp and let them download the fresh-made file from an anchor tag.
Thing is, I' don't want to create this file on server, as I have to dispose it afterwards, but I still want to give the user the "save as" window. So, is there a way, putting for example an OutputStream object in session, to achieve this result?
Thank you all!
A servlet can generate any type of content. So, when you click the button to run the servlet, simply have the servlet write the file back to the client at that time. You'll need to set the Content-type header to "text/csv" (and making sure that you set encoding properly). You don't need to set the Content-Length header; the browser can deal with that.
When the servlet returns data to the browser, the user will be prompted to save the file or open it with an application.
Yes. You don't need to save the file. Just hold it in the session and send it in-memory to the OutputStream. I would trash it after a given time or after delivery in order to save memory.
Set the content type and the content disposition appropriately. This will mean that the browser interprets the output correctly and prompts your user to save it or launch the appropriate application.
See this SO answer for more details.
So I'm making a program for android that tries to download something from www.wupload.com. What I want isn't a browser but to interact with the webpage without actually showing it. Like how HtmlUnit is supposed to work.
I'm using apache for the html requests and what I've done so far is send a post that simulates clicking on slow download on the web page. Then I read the response so I can get some variables needed to make the next post and execute the next post. In theory, the web page should be showing the captcha cause the response I get is please enter the captcha, but no image url.
The next step would be to enter the captcha and finally download the file, the problem I'm having is I don't know how to show the captcha image to the user. Do I have to capture it somehow? I know how to make the post to send what the user would type, but the image url of the captcha isn't in the source code.
I thought of inspecting the web page so I could get the url from the DOM tree, like what inspect element on google chrome does, but I have no idea if it's even possible. Any ideas would be great.
thx
The captcha is probably generated using JavaScript. Therefore when you get the source of the website, the captcha hasn't yet been generated and you won't see the image in the source HTML. You would need to run the Javascript somehow. You could try using a WebView because it has built-in support for Javascript, or get a Javascript library for Java and use it somehow. I think it would be a lot of work.
Edit:
Actually, if they are using a thirdy-party captcha library, I'm sure it uses some sort of HTTP request system, so you might be able to inspect it with this plugin for Firefox.
I have a download servlet which generates a ZIP with some files, one of them pretty big, and then sends the generated file in the response for download.
The problem is that the generating process is pretty big, and between the step of the generation of de ZIP and the download step (when the user see the download dialog) many seconds or even a minute could pass. So I would like to inform the user by anyway that the file is being generated.
The solution I'm thinking about is doing several requests, one for open a modal window which informs the user and inside this, then do another request automatically to an action which generates the file in a temp location and then, when the request ends, another one which closes the window (the window must be closed automatically) and then request to the download servlet the generated file in the last step.
If somebody understands what I'm trying to do, I would like if exists a better and cleaner solution.
You can fire an ajax request which will start the generation process, and then other ajax requests to poll the server if the file is ready. If yes - change the location of the browser to the file. Otherwise, show a "loading" message/image/..
can i upload a file(image) without submitting the form..
Basically i want to create a webpage where a user can upload an image and preview it side by side but this should not submit my form.
i dont wanna use jquery
You can have two forms on the page. One to upload the image and other one for something else. Just don't nest them.
I've tried to implement this with AJAX but, if it's even possible, it would be a nasty hack. About the best you can do reasonably is to put the file upload form in an iframe, and after posting the file with AJAX, redraw the page with the image.
I think this is what you want But it uses PHP for sever side you can use what ever you want!
All you have to do is create a page which will handle the ajax call, which is saving the image and returning string "true" in case of succeeds. and for Java this (Handling Form-based File Upload with Java Servlet or JSP) should help you.
Check this one out ioncannon.net
You can try this .Also check