0

I have to generate file depending on input (checkboxes) and download it:

[HttpGet]
    public FileResult GenerateFormatSettingsFile(IEnumerable<string> values)
    {
        var content = FileSettingsGenerator.Generate(values);
        MemoryStream memoryStream = new MemoryStream();
        TextWriter tw = new StreamWriter(memoryStream);

        tw.WriteLine(content);
        tw.Flush();
        tw.Close();

        return File(memoryStream.GetBuffer(), "text/plain", "file.txt");
    }

And on my view this:

<button id="GenetateFormatSettingsFile" class="btn btn-primary"  data-dismiss="modal"  style="margin-right: 1500px">Generate</button>

$(document).ready(function() {
        $("#GenetateFormatSettingsFile").click(function() {
            var f = {};
            var checkboxes = [];
            $('input:checked').each(function() {
                checkboxes.push($(this).attr("value"));
            });
            f.url = '@Url.Action("GenerateFormatSettingsFile", "Home")';
            f.type = "GET";
            f.dataType = "text";
            f.data = { values: checkboxes},
                f.traditional = true;

            f.success = function(response) {

            };
            f.error = function(jqxhr, status, exception) {
                alert(exception);
            };
            $.ajax(f);
        });
    });
</script>

The problem is the download doesn't start. How could I fix it?

If I do it with Html.ActionLink, the download starts, but I can't pass the values of checkboxes which is done by my ajax function above

Thanks!

Edit - that's how my check-boxes looks like:

<div class="container">
<div class="row">
    <div class="col-xs-12">
        <div class="modal" id="formatterSettings" tabindex="-1">
            <div class="modal-dialog">
                <div class="modal-content">
                    <div class="modal-header">
                        <button class="close" data-dismiss="modal">&times;</button>
                        <h4 class="modal-title">Settings</h4>
                    </div>
                    <div class="modal-body">
                        <form>
                            <div class="form-group">
                                <label for="defaultG">To default view</label>
                                <input class="form-control" type="checkbox" value="Default" id="defaultG">
                            </div>
                            <div class="form-group">
                                <label for="extendedG">To extended view</label>
                                <input class="form-control" type="checkbox" value="Extended" id="extendedG">
                            </div>
                            /div>
                        </form>
                    </div>
                    <div class="modal-footer">
                        <div class="col-md-6">
                            <button id="GenetateFormatSettingsFile" class="btn btn-primary"  data-dismiss="modal"  style="margin-right: 1500px">Generate</button>
                            @Html.ActionLink("Generate!", "GenerateFormatSettingsFile")
                        </div>
                        <div class="col-md-6">
                            <a  class="btn btn-primary" data-dismiss="modal" >Generate From Code</a>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

  • Ajax is not the best solution for a file download! Do a normal request – Shyju Jun 17 '18 at 18:05
  • @Shyju then I can't gather the checkboxes info –  Jun 17 '18 at 18:06
  • You can. How are you rendering the checkboxes ? – Shyju Jun 17 '18 at 18:08
  • @Shyju added above –  Jun 17 '18 at 18:16
  • You need to give each checkbox a `name="values"` attribute if making a normal post. Refer also [Download Excel file via AJAX MVC](https://stackoverflow.com/questions/16670209/download-excel-file-via-ajax-mvc) –  Jun 17 '18 at 22:49

1 Answers1

0

Put a submit button in your form and POST your form synchronously (without ajax). when a form is posted, values of all input elements (TextBoxes, CheckBoxes, ...) are sent to the server (with the request) and you don't need to do anything.

  • The only issue with this approach is the page reloads. It is more pleasing to the eye of the user when you just use an Ajax call instead. – IyaTaisho Jun 19 '18 at 12:55
  • @IyaTaisho, You cannot download a file using ajax! –  Jun 19 '18 at 22:30
  • I wasn't necessarily talking about downloading a file. The comment provided was talking about pulling in the data from the page (textboxes, checkboxes, etc). You don't have to use a form to pull that data is what I was speaking about, not the file download portion. The download portion, yes, you cannot pull using Ajax. – IyaTaisho Jun 20 '18 at 14:26