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

  • SEARCH
  • Home
  • 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 8123707
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T06:15:44+00:00 2026-06-06T06:15:44+00:00

I am trying to dynamically request a file for download via a GET ajax

  • 0

I am trying to dynamically request a file for download via a GET ajax call. I have the following client-side code:

$(function() {
    $('#submit').click(function() {
        $.ajax({
            type: "GET",
            url: "/download/" + "filename",
            dataType: "json",
            contentType: "application/json",
            complete: function() {
                console.log('Complete');
            },
            success: function() {
                console.log('Success');
            },
            error: function() {
                console.log('Error');
            }
        });
    });
});

On the node server I also have the following line (mind you, I have no idea if this is the correct way to do this)

app.get('/download/:filename', function(req, res) {
    console.log(req.params);
    res.write(fs.readFileSync(__dirname + "/../public/javascripts/main.js", 'utf8'));
});

So I want to actually download that javascript file (eventually dynamically). How would I go about doing this correctly? Something tells me I need to be specifying the headers / content type.

  • 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-06-06T06:15:46+00:00Added an answer on June 6, 2026 at 6:15 am

    You should return with the content type and disposition set to:

    Content-Type: application/octet-stream
    Content-Disposition: attachment;filename=\"main.js\"
    

    I believe you’ll have to redirect to this file though. If I’m not mistaken, you can’t force a file download via an AJAX call.

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

Sidebar

Related Questions

I am trying to dynamically generate JavaScript with just a URL get request. I
I'm trying to get the following done: A HTTP request comes into an address
When trying to dynamically load a Javascript file using jQuery I keep getting a
Trying to use javax.imageio.spi.ServiceRegistry to dynamically register request processors for an HttpServlet : private
The following code I am using to dynamically update the TextArea element in my
After dynamically creating a client-side table based on SELECT information retrieved from MySql database,
I am trying to stream dynamically generated data to a client over HTTP using
I'm trying to dynamically rename a jpeg image file as it's displayed to the
I'm trying to do a simple AJAX request using link_to :remote option and display
I'm trying to get a user-level program to communicate with the kernel via /proc.

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.