How to tell if new location is forcing file download? - javascript

I am redirecting user using window.location.href to a page that forces a file download using the following headers:
Content-Description: File Transfer
Content-Disposition: attachment; filename="foo"
As a result, user is not taken to the page, though asked if he wishes to proceed downloading the file.
How do I tell when the response from the server is received?
The user flow that I am trying to achieve:
Display loader.
"redirect" the user (with window.location.href).
Hide loader when response is received.

From your client side code, also pass an generated ID to the server, asking it to set a cookie for you to that value.
Your server side code should pass the cookie back with the downloaded file (in the HTTP headers).
Then you can poll for the cookie in your javascript after the redirect to see when the server has responded with a successful connection.

Related

CORS issue while trying to redirect from POST method to frontend page with GET request

I would like to redirect from POST method to frontend page with HTTP status code 303.
Expected result is that browser after making POST request redirects to page specified in Location header.
Currently I am getting CORS failed error message and browser does not redirect to frontend page.
A redirect does not mean "Load this URL in the browser window". It means "You can get whatever you asked for here".
When you make an Ajax request using JavaScript, the response is provided to JavaScript.
If the response is a redirect, then the browser follows it automatically and provides the response to the redirect to JavaScript.
The URL you redirect to needs permission from CORS in order for the JavaScript to read the response.
Do not attempt to mix web services and regular page navigation
If you want to submit some data and load a new page: Use a form submission.
If you want to submit some data and handle the response with JS: Use Ajax.

Spring REST-JWT-JavaScript navigate between secure pages

I have a Java Spring REST back-end (#RestController) and using as security option Json Web Tokens(no session). At the front-end I want to use JavaScript (jQuery to send requests to back-end), Html.
So after login I save a JWT in browser and send it back in header with every request I make to #RestController.
My question is: How to navigate between pages (that are accessible only for authenticated users) from js? How #RestController will work in this case?
The #RestController handle the requests containing a path (the path of the request is not necessary to be the same with the path(URL) from front-end)
Solution (if you have a front-end server): When you try to reach a front-end URL you make a call to the server-side; if the response status is 200 the page can be displayed(with the body of the response if you send information); if the response is not you will stay on the home page, or you can redirect the user to login page...
Also check this : http://www.studytrails.com/frameworks/spring/spring-security-method-level/

Download XML from REST endpoint with AngularJS

How can I download an XML file when I call a REST endpoint (which responds in XML format) with AngularJS?
So the flow is simple, I have created a button on the UI which makes a call to a REST endpoint (which has a response in an application/xml format) and its response should come as a download on the UI.
If I understand the question correctly (use the browser's download functionality when the user clicks the link), then this is not a question about angular really.
In order to cause a file to download you need to do two things:
Attempt to navigate the browser to the URL that returns the XML (i.e. don't make an AJAX request for it).
e.g. <a href="http://myserver.com/my/REST/endpoint>Click here</a>
Ensure the XML content is being served with headers that would force a download. If you don't do this, the browser may attempt to render the XML itself rather than downloading it. You could try either setting the Content-Type header to be applicaton/octet-stream or look into using the Content-Disposition header:
Content-Disposition: attachment; filename=someFileName.xml;

When will a cookie from server response set in Browser?

When will the cookie from server response will be set to client browser? Suppose I have a get request from client side and it returns a larger file, then when will be the cookie will be set to client. Before the download complete or after ?
ie, If it requires 30 seconds to load the complete file, then when can I access that cookie from client side (javaScript)?
Any Ideas?
The cookie will be set in the browser immediately once the Set-Cookie response header is arrived, not when the last byte of the response has been arrived. Thus, at exactly the same moment as when the Save As dialogue shows up in case of Content-Disposition: attachment.
So, whenever you'd like to track download progress, setting such a cookie is only useful when you want to poll for the "start" event of the download. Generally, in those situations when it can take seconds or even minutes to prepare the file download before it's being streamed.

307 redirect - mime type issue

We have this authentication procees in our application that whenever the server gets a request from a client which is not authenticated (cookie missing) it returns a 307 response and the client should be redirected to the login page (which might on another domain).
I've bumped into the following problem:
When I delete the cookie and the main index.html page is cached and I open the web page I get 307 for the first time only when a js file is requested (there is a script tag in the index.html). In this case the browser tries to redirect and I'm getting the following error:
Resource interpreted as Script but transferred with MIME type text/html: "https://win-7-64-server:8443/cas-server/login?service=http://172.30.2.25:81%2F172.30.2.25%3A8182%2Fshunra%2Fcommon%2Fjs%2Flibs%2Frequire%2Frequire.js".
This is the original javascript url: http://172.30.2.25:8182/shunra/common/js/libs/require.js
Obviously I don't control the redirect. Any ideas?
EDIT:
I thought about the problem and the problem is that the browser gets 307 http code and thinks that the javascrip file was redirected while in reality we want to redirect the entire website - any ideas how to do that?

Categories