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 8239899
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T20:18:13+00:00 2026-06-07T20:18:13+00:00

My current scenario is that the JavaScript client has a bunch of data that

  • 0

My current scenario is that the JavaScript client has a bunch of data that I POST to the server to process/translate into different formats (e.g. CSV), now I want to send that transformed data from the server to the client.

I set the content type of the response, but the browser does not generate a file dialog.

Here is what my controller looks like:

@RequestMapping(value="/exportBoxes/{type}/{filename:.+}", method=RequestMethod.POST)
public String exportBoxes(@RequestBody String body,      @PathVariable String type,
                          @PathVariable String filename, HttpServletResponse response) throws IOException
{
    JsonObject jsonObject = new JsonParser().parse(body).getAsJsonObject();

    //grab the data from the JSONobject
    String data = jsonObject.get("JSONdata").getAsString();

    //create output stream writer
    PrintWriter p = new PrintWriter(response.getOutputStream());

    //set response type and print header
    if(type.equals("csv"))
    {
        response.setContentType("text/csv");
        response.setHeader("Content-Disposition", "attachment; filename=\"" + filename + "\"");
    }

    //print the points to the file
    for(int i = 0; i < splitPoints.length; i++)
    {
        //print remainder of CSV file - abstracted
    }

    p.flush(); //flush the stream
    response.flushBuffer();
    p.close(); //close the stream

    return "success";
}

And here is the client function that POSTs the data:

DAService.prototype.exportBoxes = function(type, filename, data) {
    var path       = 'api/rest/da/exportBoxes/' + type + '/' + filename
    var url        = (this.connection) ? this.connection + path : path;
    var JSONdata   = '';
    var returnType = ''

    //create JSON string to pass to Java controller
    if(type == 'csv')
    {
        JSONdata   = '{ "JSONdata" : "' + data.replace(/ /g, '') + '" }';
        returnType = 'text/csv';
    }
    else
    {
        throw "Invalid export format " + type;
    }

    $j.ajax({
        url:         url,
        contentType: 'application/json',
        type:        'POST',
        dataType:    returnType,
        data:        JSONdata,
        success: function(returnedData){
            console.log("exportBox successful");
        },
        error: function(x,y,z) {
            console.log("exportBox failed with error '" + y + "'");
        },
        complete: function(empty, textStatus){
            console.log("exportBox complete textStatus='" + textStatus + "'");
        }
    });
};

No errors are generated by this code and the server’s response has the CSV file in it, I just can’t get the client to generate the download dialog.

There is one piece that I am missing that I don’t see, can anyone help me out?

  • 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-07T20:18:15+00:00Added an answer on June 7, 2026 at 8:18 pm

    You might want to try posting a form instead of using $.ajax:

    var form = $('<form/>', {
      action: url,
      method: 'POST',
      css: { display: 'none' },
      html: $('<input/>', {name: 'JSONdata', value: data.replace(/ /g, '') })
    });
    $('body').append(form);
    form.submit();
    

    (I haven’t tested that.) The point is that when you really post a form, the browser knows to interpret the response body, and it should notice your attachment.

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

Sidebar

Related Questions

Current scenario, I launch a process that forks, and after a while it aborts().
my current scenario is this -> i am showing a view controller that has
Our current scenario is like this: we have an existing database that needs to
This is scenario that I ran into a few times: I copy some text
My current scenario is that I have 20 Excel files that need to be
The current situation is that there is a databound data grid view that many
Here's the scenario: You have a Windows server that users remotely connect to via
This is my current scenario: I have checked in my grails app code +
The current issue im facing is comes from the following scenario. I have a
We have a scenario where some .NET code is attempting to access the current

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.