How to download a file by jQuery.Ajax?

0 votes

I have a Struts2 action in the server side for file downloading.

<action name="download" class="com.xxx.DownAction">
    <result name="success" type="stream">
        <param name="contentType">text/plain</param>
        <param name="inputName">imageStream</param>
        <param name="contentDisposition">attachment;filename={fileName}</param>
        <param name="bufferSize">1024</param>
    </result>
</action>

However when I call the action using the jQuery:

$.post(
  "/download.action",{
    para1:value1,
    para2:value2
    ....
  },function(data){
      console.info(data);
   }
);

in Firebug I see the data is retrieved with the Binary stream. I wonder how to open the file downloading window with which the user can save the file locally?

Sep 18 in Web Development by kartik
• 31,530 points
319 views

1 answer to this question.

0 votes

Hello @kartik,

You don't need to do this through Ajax. Just use

window.location="download.action?para1=value1...."

Hope it helps!!

Thank You!!

answered Sep 18 by Niroj
• 68,260 points

Related Questions In Web Development

0 votes
1 answer

is there any way to download truffle? how to solve this error

Hello @ aishah , It seems likely that this is ...READ MORE

answered May 25 in Web Development by Niroj
• 68,260 points
86 views
0 votes
1 answer

How to track with Google Analytics on a redirection page with PHP?

Hello @kartik, Since the page that is sending ...READ MORE

answered Jul 7 in Web Development by Niroj
• 68,260 points
246 views
0 votes
1 answer

How to replace html element with ajax response?

Hello @kartik, Assuming you are replacing your products, ...READ MORE

answered Jul 7 in Web Development by Niroj
• 68,260 points
312 views
0 votes
1 answer

How to create a laravel hashed password?

Hello @kartik, Hashing A Password Using Bcrypt in Laravel: $password ...READ MORE

answered Oct 6 in Web Development by Niroj
• 68,260 points
23 views
+1 vote
1 answer

How to make anchor tag with routing using Laravel?

Hey @kartik, First you have to go to ...READ MORE

answered Mar 18 in Laravel by Niroj
• 68,260 points
2,893 views
0 votes
1 answer

What is redirection in Laravel?

Named route is used to give specific ...READ MORE

answered Mar 18 in Laravel by Niroj
• 68,260 points
351 views
0 votes
1 answer

How to install Laravel via composer?

Hello, This is simple you just need to ...READ MORE

answered Mar 23 in Laravel by Niroj
• 68,260 points
240 views
+1 vote
1 answer

What are named routes in Laravel and How can specify route names for controller actions?

Hey @kartik, Named routing is another amazing feature of ...READ MORE

answered Mar 23 in Laravel by Niroj
• 68,260 points
3,580 views
0 votes
1 answer

How to deny direct access to a folder and file by htaccess?

Hello @kartik, Just move the includes folder out of the ...READ MORE

answered Aug 23 in Web Development by Niroj
• 68,260 points
214 views
+1 vote
1 answer

How to access the Angularjs scope of a particular html element from our console?

Hello, You should follow the below steps:-- 1.Compile and ...READ MORE

answered Jan 21 in Web Development by Niroj
• 68,260 points

edited Jan 21 by Niroj 100 views