c# - 将 JavaScript/JSON 数组传递给 MVC 'GET' 方法

标签 c# jquery asp.net .net asp.net-mvc-3

我正在尝试将 Guid 的数组/IEnumerable 传递给 MVC“GET”方法,如下所示:

[HttpGet]
public ActionResult ZipResults(IEnumerable<Guid> ids)
{
    using(var zip = new Zip())
    {
        foreach(var id in ids)
        {
            var stream = GetDataStream(id);
            zip.AddEntry("filename.txt", stream);
        }
    }

    var outputStream = new MemoryStream();
    zip.Save(outputStream);

    return FileStreamResult(outputStream, "application/octet-stream"){
        FileDownloadName = "Results.zip" };
}

我的 javascript 看起来像这样:

$('the-button').click(function(){

    // 1. Get the guids from a table and add to javascript array (works fine)
    // 2. Grey-out screen and show processing indicator (works fine)

    // 3. This is how I'm calling the "ZipResults" action:
    $.ajax({
        url: '@Url.Action("ZipResults", "TheController")',
        type: 'GET',
        data: $.toJSON({ ids: _ids }),
        dataType: 'json',
        contentType: 'application/json;charset=utf-8',
        traditional: true,
        success: function(){
            // Undo the grey-out, and remove processing indicator
        },
        error: function(){
        }
    });
});

我的预期是这会在浏览器上弹出下载对话框。实际上,传递给 Controller ​​的 javascript 数组为空(在服务器端,它在客户端正常工作)。此外,这适用于“POST”,但是,以这种方式使用的“POST”方法不会强制下载对话框...

欢迎提出建议:)

最佳答案

您应该避免使用 GET 发送 JSON 请求。像这样尝试:

var _ids = [ 
    'e2845bd4-9b3c-4342-bdd5-caa992450cb9', 
    '566ddb9d-4337-4ed7-b1b3-51ff227ca96c',
    '25bc7095-a12b-4b30-aabe-1ee0ac199594'
];

$.ajax({
    url: '@Url.Action("ZipResults", "TheController")',
    type: 'GET',
    data: { ids: _ids },
    dataType: 'json',
    traditional: true,
    success: function() {
        // Undo the grey-out, and remove processing indicator
    },
    error: function() {

    }
});

话虽这么说,我看到您正在调用一些 Controller 操作,该操作返回要下载的文件流。您根本不应该使用 AJAX 来执行此操作。这样做的原因是,在您的成功回调中,您将获得 ZIP 文件的内容,但您无能为力。你不能将它保存到客户端计算机,你不能提示用户选择保存位置,你几乎完蛋了。

因此,如果您要下载文件,则不会调用 AJAX。您可以使用一个简单的 anchor :

@Html.ActionLink("download zip", "ZipResults", "TheController", null, new { id = "download" })

然后:

$(function() {
    $('#download').click(function() {
        var _ids = [ 
            'e2845bd4-9b3c-4342-bdd5-caa992450cb9', 
            '566ddb9d-4337-4ed7-b1b3-51ff227ca96c',
            '25bc7095-a12b-4b30-aabe-1ee0ac199594'
        ];

        var url = this.href;
        for (var i = 0; i < _ids.length; i++) {
            if (url.indexOf('?') > 0) {
                url += '&ids=' + encodeURIComponent(_ids[i]);
            } else {
                url += '?ids=' + encodeURIComponent(_ids[i]);
            }
        }

        window.location.href = url;

        return false;
    });
});

关于c# - 将 JavaScript/JSON 数组传递给 MVC 'GET' 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9113995/

相关文章:

c# - 如何在 Controller 之外使用 EF DbContext?

asp.net - 尖锐的架构问题

c# - 抓取 JavaScript 动态生成的数据

c# - 如何使用 C# 客户端在 Azure Synapse 上创建表?

javascript - 如何根据属性Id获取内部html

javascript - jQuery 选择器 : 'starts-with && ends-with' doesnt work on element with multiple classes

c# - 使用c#保持隐藏的输入框名称相同

c# - 将同一个表中的 2 个查询合并为一个 linq 查询

c# - 如何触发http 1.1 4xx 5xx错误码? C#

jQuery Deferred - 等待多个 AJAX 请求完成