Send file download request using JQuery POST - java

I am rewriting some code that used to use GET and replaced it with POST.
The download URL used to be a GET request to
https://myurl/getfile?fileid=1234&filetype=pdf
Now, I changed that to
https://myurl/getfile
and put the fileid=1234&filetype=pdf in the POST body.
I did this using jquery's post method as:
function postCall(url, param) {
$.post(url, param);
}
The server side is written using Java and I tried to reused the old code for GET, which write the file binary into the servlet's stream.
However, my browser does not prompt user for download, which used to do for GET.
Previous posts on stackoverflow did suggest that AJAX should not be used for file download. But what is the alternative way for me to use? The request is not generated by a form though.
Many thanks.

I would suggest creating a form on the page (or create one dynamically using jQuery), and then have that form do the post submission (using jQuery's "submit" function or "trigger('submit')" on the form). This way the request won't be done asynchronously in the background. If the "getfile" script responds with a file with Content-disposition: attachment, it should download.
That said, I'm not sure the browser will "prompt" the user in this scenario--this is dependent on the browser (whether or not a dialog appears to save the download, or if it automatically downloads the file without a prompt).

Related

how can I force a js file download to occur via my Java servlet?

In my organization, all http apps undergo a security check. There are a few headers I need to add to each response via the servlet, that's easy.
But, I have a new servlet, which returns html code as a response. part of the html is:
<script type=\"text/javascript\" src= \""+_pathToScript +"myScript.js\"/>
now, the browser gets this response and then generates another GET request for the file. this request is not passing via my servlet and thus I can't add the needed headers to this response.
I tried to change the src value to something like /servlet/MyServlet?GetJS, but the browser doesn't seem to understand that and it doesn't trigger the download.
Any suggestions?
Im running on IBM WebSphere.
Thanks a lot!

How to get the response stream of browser in Java WebDriver program

I need to validate the PDF report. I need to get the report embeded in HTML. If I read that URL using:
File file = new File("url");
or
HttpWebConnection.getResponse();
it requests the URL in separate session, hence it cannot get the file.
Does ieDriver have something like HtmlUnit?
HttpWebConnection.getResponse()
or somebody can suggest alternative.
Unfortunately it does not.
If you want to get the response code you will need a proxy. If you are using Java then Browser Mob is what you need. You may also try making XmlHttpRequest from javascript and get the status code in that way.
You could also stick to the method you are using right now (separate request from Java) but pass the cookie with session (you can obtain the cookie from WebDriver)

Java servlet - Forwarding/Redirecting from subpage

I am working on a project where I have running servlets packaged in a war that listen for requests and populate a foreign div on a separate page. An initial request is made to servlet (a) and if there is data to display, it renders that HTML. If there is NO data, it passes a query string to another servlet to handle the request and then renders a page with options to choose.
I am running Tomcat 6 with Windows Server 2008.
But I run into two problems:
When I use redirect, I get no response from the servlet being
redirected to. I have some javascript alerts up that are never called by the (b)
servlet. I AM using relative paths and confirm the link is
correct in logs.
When I copy that link in step 1 in a new window, I see
the results. Just not when it's embedded in another page that makes
the request. Why would that be? Is it possibly a limitation from the host page and not being able to render the response?
When I use a forward, I see the servlet response,
but then a new window opens. Thus taking the person away from the
original page. This would be great if the results render in the same page.
What's the best practice to assure that I can "redirect" from an initial servlet call to another servlet using the response object from that first servlet?
A redirect returns a HTTP 302 response with the new URL in Location header which the client needs to deal with. Basically, your JS code must check the response status code if it's 302 and then extract the Location header and then re-send a new request on it. Repeat this until the response status code is 200.
That it works when pasting the URL in browser's address bar is because the browser already knows how to deal with 3nn responses properly. If you open up the network traffic tracker in browser's webdeveloper toolset, then you'll see that a second GET request is been fired on the new URL.
Another way, if the servlets run in the same container, is to just use RequestDispatcher#forward() instead of HttpServletResponse#sendRedirect().

HTML form post Vs PHP form post

I currently have to post HTML form to a java server like ( www.example.com/file.do ).Java server accept only HTML request. So whenever i post request with html file its work fine. But whenever i post it from php its not working ( same html file just change the extension to php). I have also try with mod-rewrite by changing php to html with .htaccess but nothing happen still not works. So i am thinking is there any difference in html form post and php form post? Or is there any other solution so i can post html form through php to java?
Thanks to all.
I think you are posting from localhost. Try to post from a live server. Hopefully this will solve your problem.
technically there is no difference.
Check whether there are some special http headers set, which are not accepted by your server, i.e.
X-Powered-By etc.
To create a POST request from a PHP script, you need to use curl; but to submit a form from the browser, it doesn't matter what the extension of the file that is rendered is.

How to enable downloading of dynamically generated files from a browser?

I have a web application in which one of the workflows, users can download files that are dynamically generated. The input is a form which has parameters needed to generate the file.
My current solution is to let them submit this form & on the servlet side I change the response header - content disposition to be an attachment & also provide an appropriate mime-type.
But I find this approach to be inadequate. Because there are chances that the generation of file can take a very long time, in such cases after a certain timeout I directly get 500 or 503 errors in the browser. I guess this is to be expected for the current approach.
I want my workflow to be flexible enough to tell the users as soon as they submit the form that it might take time for the file to generate & that we will display the link to the file as soon as it is ready. I guess I can also email the file or this message to them, but this is not ideal.
Can you guys suggest me an approach for this problem? Should I be more specific in providing information? Any help appreciated.
If you want to do this synchronously (i.e. make the user wait for the document to be ready rather than have them go off and do other things while waiting) a traditional approach is to bring them to a "report loading" page.
This would be a page that:
1) informs them that the report is loading.
2) refreshes itself (either using the meta refresh tag or javascript)
3) upon refresh, checks to see if the report is ready and either:
a) goes back to step 1 if it isn't ready
b) gives them the document if it is ready.
Synchronous is kind of old-school, but your question sounded like that was the approach you wanted.
Asynchronous approaches would include:
Use Ajax to make a link to the document appear on the page once it is ready.
Have a separate page that shows previously generated documents. The use can go to this page at their leisure, and, meanwhile, they can browse the rest of the site. This requires keeping a history of generated documents.
As you suggested, send it via e-mail.
You can make an asynchronous Ajax Call to the server with the form data instead of submiting the form direct.
On the server you create a temp file and return a link to the client with the download URL.
After submitting the answer via Javascript you can show the user a hint, that the download link will appear in a minute. Don't forget to cleanup the temp file!
For submitting the Ajax Call I would suggest using an Javascript Framework. Have a look at JQuery:
http://api.jquery.com/category/ajax/

Categories