2

I am trying to get a zip file (or an array of bytes instead) from action controller and unzip it in javascript. The aim is to save time because data size I want to download is really big.

This is the action controller which returns data:

    public FileContentResult GetKML()
    {
        byte[] strResult = GetZippedDataWithDotNetZip();
        return File(strResult, "application/zip", "test.zip");
    }

And I call that controller action in javascript:

$.post('Home/GetKML', {}, function (data) {
    var info = unzip(data);
});

The problem is the controller action is returning undefined. Why?

J punto Marcos
  • 437
  • 1
  • 6
  • 24

1 Answers1

1

The problem is likely due to the fact that jquery's ajax functions only handle response types of xml, html, script, json, jsonp, and text (I haven't verified this, but it's based on my prior experiences with it). You are returning binary data from your action, which they can't process.

I strongly doubt that the controller action is returning 'undefined' as that is a javascript idiom. You are probably just trying to do a console.log of the return value and are seeing 'undefined', in which case you are looking at the end result of jquery's failure to process the Action's output. You'll get better troubleshooting info if you use Firefox and Firebug to look at the actual response from the ajax request.

That answers the "why?" in your question, I believe.

If you want some additional options, and based on your responses I think you do, since you're so keen on performance and aren't interested in using the already-supported infrastructure which is available to transparently enable exactly the kind of performance enhancement you want to do (i.e., gzip compression), you are quite welcome to roll your own jquery plugin that handles the binary data coming back from the server and does whatever you want. Your desire for self-implemented performance enhancements makes me suspect you will be willing and able to dig into the jquery documentation to achieve that. Or if that fails, why not re-implement the jquery functionality in your own way to support the extraction of the zip data?

Another option is to send back the zipped up file as a mime-encoded string, which will, of course, nullify the usefulness of the zipped file. But, then again, you could enable gzip compression and it would get compressed after all.

theta-fish
  • 189
  • 1
  • 7