通过ASP.NET MVC下载文本文件动态更改

时间:2018-06-17 18:05:17

标签: asp.net ajax asp.net-mvc asp.net-mvc-4 razor

我必须根据输入(复选框)生成文件并下载它:

[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");
    }

我认为这是:

<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>

问题是下载没有启动。我怎么能解决它?

如果我使用Html.ActionLink,下载开始,但我无法传递复选框的值,这是由我的ajax函数完成的

谢谢!

编辑 - 这就是我的复选框的样子:

<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>

1 个答案:

答案 0 :(得分:0)

在表单中放置一个提交按钮,同步 POST 您的表单(不带ajax)。 发布表单时,所有输入元素(TextBoxes,CheckBoxes,...)的值都会发送到服务器(带有请求),您无需执行任何操作。