0

I have an ajax post request:

  function downloadElevationMap() {
    var jsonData = ko.toJSON(mod.SelectedItem);
    $.ajax({
        url: '/Home/GetData/',
        type: 'POST',
        contentType: "application/json; charset=utf-8",
        dataType: 'json',
        data: jsonData,
        success: function (data) {
            console.log("OK");
        },
    });
}

The controller method recive data correct. The code is presented below:

public FileStreamResult GetData(Mv jsonData)
        {
            var resultAll = jsonData.Data.Select(d => d.y).ToList();
            var str = new StringBuilder();
            _fileName = "ses"+jsonData.Path;
            foreach (var d in resultAll)
            {
                str.Append(d + " ");
            }
            var byteArray = Encoding.ASCII.GetBytes(str.ToString());
            var stream = new MemoryStream(byteArray);
            return File(stream, "text/plain", string.Format("{0}.txt", _fileName));
        }

Mv - is my class that represent data. When debug the both str and stream variable contain correct data.

Function downloadElevationMap() is called by onclick="downloadElevationMap()"

I just want when downloadElevationMap() is called the GetData controller return a file for download. But simply nothing happend. Where the error is?

user2892082
  • 3
  • 1
  • 3

2 Answers2

2

well you dont need ajax to do that try this

window.location="download.action?para1=value1...."

for your needs you can do some thing like this

window.location="/Home/GetData/?"+$.param(jsonData )
Anto Subash
  • 3,140
  • 2
  • 22
  • 30
  • But my jsonData is really huge. It is impossible to pass it in that way, i think. Another problen that file for downloading generates depends on jsonData. – user2892082 Oct 22 '13 at 19:13
1

I'm fairly sure what you are doing is swallowing the data in the success callback in the AJAX call - all your code will do is download the file, call the success callback, and then just print "OK" to the console.

As Anto said, you don't need AJAX (and, indeed, should not be using AJAX) for this. His answer is absolutely correct, provided you can use a GET string. If you need to use a POST request, create a form with hidden inputs and submit that - something like:

HTML

<form action="/Home/GetData" method="POST" id="dataToSubmit">
   <input type="hidden" name="param1" value="value1" />
   <input type="hidden" name="param2" value="value2" />
   <input type="hidden" name="param3.param4" value="value3" />
</form>

JS

function downloadElevationMap() {
  // Write code to map your jsonData to your form elements

  $('#dataToSubmit').submit();
}

You could make the form dynamically if you wish. You might be able to update your page to post directly with a submit button.

One final note, you don't need to submit the data as Json. If you have

{
    "param1": "value1",
    "param2": "value2",
    "param3": {
        "param4": "value3"
    }
}

then if you just use the format in the form above, it will submit fine - this reference explains how to submit to deep models.