Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6063193
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T09:08:17+00:00 2026-05-23T09:08:17+00:00

I am currently attempting to have the browser automatically prompt the user to save

  • 0

I am currently attempting to have the browser automatically prompt the user to save a file from server. I have a Java Servlet coded as follows:

private void doDownload( HttpServletRequest request, HttpServletResponse response){
    File f = new File(<filename.ext> //This is text file but I have tried with pdfs, gifs, zips
    ServletOutputStream op = response.getOutputStream();
    int length = 0;
    op = response.getOutputStream();
    String mimetype = context.getMimeType( f.getAbsolutePath() );
    resp.setContentType("application/x-download");
    response.setContentLength( (int)f.length() );
    response.setHeader("Content-disposition", "attachment; filename=<newFileName.ext>");
    byte[] bbuf = new byte[8192];
    DataInputStream in = new DataInputStream(new FileInputStream(f));

    while ((in != null) && ((length = in.read(bbuf)) != -1))
    {
        op.write(bbuf,0,length);
    }

    in.close();
    op.flush();
    op.close();
}

I am using Firefox to do the testing and I have Firebug running. I can see the Firebug response contains the headers as I have set them and that when the request is for a text file, all the text that should be there is contained in the response.

On the client I have Javascript making the request asynchronously as follows:

try{
    xmlhttp = window.XMLHttpRequest?new XMLHttpRequest(): new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e)
{

} 

xmlhttp.open("post", 'myUrl?action=download', true);
xmlhttp.send(null);

I can see that the request makes contact with the server and that the response is correct, as I said above that I can see the text file etc. in the Firebug response output.

The problem is that nothing happens from here, there is simply no browser response. I have scoured the internet for the correct way to do this but all that I can find is that the server needs to set the content-disposition to “attachment; filename=” and that the contentType should be set to “application/x-download”. I have tried setting the contentType to “application/octet-stream” but nothing that I have tried seems to work.

Please can someone explain to me if there is something that I am doing wrong?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T09:08:17+00:00Added an answer on May 23, 2026 at 9:08 am

    You have to make the browser “visit” the page for the download dialog to appear. Just sending a post request is not enough.

    Probably the best way to do this with JavaScript is to dynamically make a <form> object with action="/your/servlet/page" and then use JavaScript to call submit() on the form object (don’t forget to add the <form> to the page or it won’t work).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm currently attempting to print a document from WPF. I'm using the web browser
Currently we have a hybrid ASP/PHP setup connecting to a SQL Server 2005 database.
I'm currently attempting to create a tabbed interface in a web application, and based
Currently, I don't really have a good method of debugging JavaScript in Internet Explorer and
Currently we have a project with a standard subversion repository layout of: ./trunk ./branches
Currently I'm doing some unit tests which are executed from bash. Unit tests are
Attempting to use asp.net mvc's Action Result of File. So it would seem that
I have uploaded a simple hello world on my IIS server 7 (shared hosting).
I have written a CSS server which does minimization and basic parsing/var replacement. The
I'm attempting to write a browser game using Django but I'm getting a bit

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.