Ask Questions and Find Answers
Important:
Ask is now read-only. You can review any existing questions and answers, but not add anything new.
But - don't panic! While ask is no more, we've replaced it with discuss - the new Liferay Discussion Forum! Read more here here or just visit the site here:
discuss.liferay.com
RE: Liferay DXP/7.1 AJAX File Download or Alternative
Hello Masters,
We are not able to donwload file using ajax call from liferay dxp onward. it's working upto 6.2.
Why is it so ? Can some one elaborate more on this ?
Do we have any alternative for failure message or download file working with single request ?
Thanks!!
We are not able to donwload file using ajax call from liferay dxp onward. it's working upto 6.2.
Why is it so ? Can some one elaborate more on this ?
Do we have any alternative for failure message or download file working with single request ?
Thanks!!
Ketan Savaliyawell... how exactly would you like us to point to the root cause with the amount of information that you provide?
We are not able to donwload file using ajax call from liferay dxp onward. it's working upto 6.2.
Why is it so ? Can some one elaborate more on this ?
Olaf KockKetan Savaliyawell... how exactly would you like us to point to the root cause with the amount of information that you provide?
We are not able to donwload file using ajax call from liferay dxp onward. it's working upto 6.2.
Why is it so ? Can some one elaborate more on this ?
For an example...
Lets says i have some external service return InputStream or Byte data, if successfully get this data then i want to display/download it or display erro message to user like "Not a valid request"
Also i would more like to know the technicle reason why it's stop working via ajax.
Thanks in advance!!
Ketan SavaliyaAlso i would more like to know the technicle reason why it's stop working via ajax.Well, if the answer is "works for me", that wouldn't help you much. Can you share what exactly stopped working for you? E.g. provide a minimal example to reproduce. We can't help you much if we know that it stopped working - with an unspecified "it".
Olaf KockKetan SavaliyaAlso i would more like to know the technicle reason why it's stop working via ajax.Well, if the answer is "works for me", that wouldn't help you much. Can you share what exactly stopped working for you? E.g. provide a minimal example to reproduce. We can't help you much if we know that it stopped working - with an unspecified "it".
Hi Olaf,
Already told in initial thread. But still let me give you more detail on this.
Simply I just want to know, why Download file stop working form ajax call in DXP or 7.1? Flow like jsp -> js (Ajax call) -> serveResource() for download file.
I need ajax because if something went wrong i need to show user friendly message or download file. But download not working via ajax. That is my question.
I guess, nobody here can answer that because we still have no idea what you are doing. Till you last answer I have assumed that you were downloading from the document library. Now I understand that you call serveResource. Previously nobody of us could have known.
To diagnose the issue I would do this:
Open a browser tab (Firefox or Chrome), go to the page, hit F12, and switch to the network tab. Start the ajax download.
Open a second browser tab (Firefox or Chrome), go to the page, hit F12, and switch to the network tab. Start a direct download.
Now I would look at the headers that are sent and compare, what's different, for request and response.
To diagnose the issue I would do this:
Open a browser tab (Firefox or Chrome), go to the page, hit F12, and switch to the network tab. Start the ajax download.
Open a second browser tab (Firefox or Chrome), go to the page, hit F12, and switch to the network tab. Start a direct download.
Now I would look at the headers that are sent and compare, what's different, for request and response.
Ketan SavaliyaAlready told in initial thread. But still let me give you more detail on this.Hi Ketan,
Simply I just want to know, why Download file stop working form ajax call in DXP or 7.1? Flow like jsp -> js (Ajax call) -> serveResource() for download file.
I need ajax because if something went wrong i need to show user friendly message or download file. But download not working via ajax. That is my question.
I understand your question, but I have trouble reproducing: Please give an example - preferably a minimal implementation. Code. Something that we can use to reproduce. "It stopped working" is a description of the problem that could mean anything. Do you get a 404 response code? Does your JS not trigger a request at all? We don't know, thus nobody can help you without the wildest of all guesses.
And, as I said above, it won't help you if I have working downloads with Ajax and answer "Works for me". You probably would like to fix your problems, and to help you, we'll need to understand the details of your problem, not just the abstract description.
Olaf KockKetan SavaliyaAlready told in initial thread. But still let me give you more detail on this.Hi Ketan,
Simply I just want to know, why Download file stop working form ajax call in DXP or 7.1? Flow like jsp -> js (Ajax call) -> serveResource() for download file.
I need ajax because if something went wrong i need to show user friendly message or download file. But download not working via ajax. That is my question.
I understand your question, but I have trouble reproducing: Please give an example - preferably a minimal implementation. Code. Something that we can use to reproduce. "It stopped working" is a description of the problem that could mean anything. Do you get a 404 response code? Does your JS not trigger a request at all? We don't know, thus nobody can help you without the wildest of all guesses.
And, as I said above, it won't help you if I have working downloads with Ajax and answer "Works for me". You probably would like to fix your problems, and to help you, we'll need to understand the details of your problem, not just the abstract description.
Hi Olaf,
Here is attachment files for sample code.
While i am trying download with view.jsp - Working
While i am trying download with view_without_ajax.jsp - Not Working
I looked at your code, didn't test it. I see two huge differences. One: When you download the file directly, the browser sends a GET request. You use a POST request.
The other possible culprit is the dataType json. I suspect, when you do that, jquery sends a contenttype header and the serveResource implementation does not like it/is never called.
Also: Not sure which browser you use, but I remember that we had to do some workarounds to geht $.ajax + json working in Internet Explorer.
To solve this, you will have to fiddle with the $.ajax parameters. Use the development console of the browser and look at the request/response headers. If you get a 200 response and the correct content, then the backend works fine and you will only have change some jquery parameters.
If the backend doesn't work, try to do a GET request. Does that work? Doesn't matter if you need POST, try it for diagnostic purposes. Then, you can again try POST. But remove the datatype, try just a plain post.
The other possible culprit is the dataType json. I suspect, when you do that, jquery sends a contenttype header and the serveResource implementation does not like it/is never called.
Also: Not sure which browser you use, but I remember that we had to do some workarounds to geht $.ajax + json working in Internet Explorer.
To solve this, you will have to fiddle with the $.ajax parameters. Use the development console of the browser and look at the request/response headers. If you get a 200 response and the correct content, then the backend works fine and you will only have change some jquery parameters.
If the backend doesn't work, try to do a GET request. Does that work? Doesn't matter if you need POST, try it for diagnostic purposes. Then, you can again try POST. But remove the datatype, try just a plain post.
Why are you trying to download a file via AJAX anyway?
Any standard servlet request allows for a download and, if going through a portlet, you get the resource requests.
Use the HTTP response codes for your indicators why something failed.
Done.
Any standard servlet request allows for a download and, if going through a portlet, you get the resource requests.
Use the HTTP response codes for your indicators why something failed.
Done.
David H NebingerWhy are you trying to download a file via AJAX anyway?
Because I need to either file download or showing message to user.
Can do it without ajax ? or Any alternative ?
If you only have a handful of messages, those can be triggered using the HTTP response codes.
Not everything needs to be a fully flushed out response generated solely from the backend.
Besides, notifying of a failure to download is different than "notifying the user". It almost sounds like you're trying to dual-purpose the call, checking for the viability of a download at the same time as processing the download; if the viability test passes, give the file, if it fails explain why.
Just separate them into the two distinct parts and use the viability test to show the link/button that enables the download or the failure message; don't try to force the square peg into the round hole.
Not everything needs to be a fully flushed out response generated solely from the backend.
Besides, notifying of a failure to download is different than "notifying the user". It almost sounds like you're trying to dual-purpose the call, checking for the viability of a download at the same time as processing the download; if the viability test passes, give the file, if it fails explain why.
Just separate them into the two distinct parts and use the viability test to show the link/button that enables the download or the failure message; don't try to force the square peg into the round hole.