0

I have a server using Java's Spring library that generates a .csv file based on the parameter queryParam when I do an AJAX post on the client side.

$.ajax(
{
    url: myUrl,
    type: 'POST',
    contentType: 'application/json',
    data: queryParam,
    success: function(response)
    {
        console.log(response);
    },
    error: function(jqXHR, textStatus, errorThrown)
    {
        console.error("error in query()");
        console.log(jqXHR);
        console.log(textStatus);
        console.log(errorThrown);
    }
});

The server-side code attaches the appropriate headers and writes the data directly to the response stream (please note I have to do it that way, since I cannot make a file on the server):

@RequestMapping(value = "/query", method = RequestMethod.POST)
public void exportSearchEvents(HttpServletResponse resp, @RequestBody QueryParams params)
{
    Writer os=null;

    try
    {
        resp.setHeader("Content-Disposition", "attachment; filename=\"searchresults.csv\"");
        resp.addHeader("Content-Type", "application/force-download");
        resp.addHeader("Content-Type", "application/csv");
        resp.addHeader("Content-Type", "application/x-download");

        resp.setContentLength(580);
        resp.addHeader("Pragma", "public");
        resp.addHeader("Expires", "0");
        resp.addHeader("Cache-Control", "must-revalidate, post-check=0, pre-check=0");
        os = resp.getWriter();
        createFile(os, params); //writes the .csv data to the response PrintWriter object
        os.flush();
        os.close();
    }
    catch(Exception e)
    {
        logger.info("Error creating .csv file. " + e.getMessage());
        logger.error("Error creating .csv file.", e);
    }

    logger.info("got to here. " + resp.toString());

    if(os==null)
    {
        logger.info("Error creating .csv file: null");
        logger.error("Error creating .csv file: null");
        return;
    }
    return;
}

When it returns, I get a string back of the results (that should be in the .csv), but the download option box doesn't pop up.

Also, I should mention, I cannot create the file on the server and link to it (i.e. document.location.href = /server/createdfile.csv or whatever) from the browser due to file permissions that cannot be changed. And I have also tried various forms of hidden iframes that href to various places, but since I have to somehow give queryParam to the server, I can't do that (correct me if I'm wrong).
And because of limitations with Spring, simple POSTs and GETs won't work (I'm assuming it's an issue with Spring):

$.get(myUrl, queryParam, function(response)
{
    console.log(response);
});

and

$.post(myUrl, queryParam, function(response)
{
    console.log(response);
});

give me:

The server refused this request because the request entity is in a format not supported by the requested resource for the requested method ().

Also note that I'm avoiding Flash, so please, no answers regarding Flash.

Thanks in advance.

Eric
  • 1
  • 1
  • 1
  • try making your call non-ajax... Make a simple call to your query with get params. `http://myUrl?parm1=val1&param2=val2` – Gaurav Shah Jun 15 '12 at 22:41
  • @GauravShah: I'm sure that would work, but do you know how I would pass a javascript object (json stuff) as my parameter, as opposed to just simple strings? Thanks again. – Eric Jun 25 '12 at 15:08
  • http://stackoverflow.com/questions/8447738/pass-or-return-json-object-from-jquery-get – Gaurav Shah Jun 26 '12 at 11:45

1 Answers1

0

http://gloryplus.com/index.php?route=product/product&path=81&product_id=283

$fsize = filesize($fullPath);
$path_parts = pathinfo($fullPath);
$ext = strtolower($path_parts["extension"]);
switch ($ext) {
    case "pdf":
    header("Content-type: application/pdf"); // add here more headers for diff. extensions
    header("Content-Disposition: attachment; filename=\"".$path_parts["basename"]."\""); // use 'attachment' to force a download
    break;
    default;
   header("Content-type: application/octet-stream");
  header("Content-Disposition: filename=\"".$path_parts["basename"]."\"");
}
while(!feof($fd)) {
    $buffer = fread($fd, 2048);
    echo $buffer;
}