如何处理阿贾克斯职位的FileStream返回类型? [英] How to handle FileStream return type in .ajax post?

查看:159
本文介绍了如何处理阿贾克斯职位的FileStream返回类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过以下code发送的JSON对象。在CSV格式的控制器返回的值应该是提供PROMT打开或保存为CSV文件。我无法明白,究竟是什么code应该写入成功:函数(结果)

I am sending JSON object through following code.. Controller returning values in CSV format that should be provide promt to open or save as CSV file. I unable to understand that what exactly code should be write in success: function (result)

链接出口

Html.ActionLink("Export", "", "", null, new { @onclick = "return exportData();"})

function exportData() {

var searchViewModel = getExLogSearchModelData();
var path = getAbsolutePath("ExclusionLog", "ExportData");
$.ajax({
    url: path,
    cache: false,
    contentType: 'application/json; charset=utf-8',
    dataType: 'html',
    type: 'POST',
    data: JSON.stringify(searchViewModel),
    success: function (result) {
    },
    error: function (error) {
        $("#voidcontainer").html("Error");
    }
});

}

控制器的ActionResult

Controller ActionResult

    public ActionResult ExportData(ExclusionLogSearchViewModel postSearchViewModel)
    {
        try
        {
            IEnumerable<ExclusionLogViewModel> exclusionLogData = null;
            exclusionLogcData = this._workerService.GetExclusionLogData(postSearchViewModel);

            return CSVFile(exclusionLogMembershipSyncData.GetStream<ExclusionLogViewModel>(), "ExclusionLogTables.Csv");
        }
        catch (Exception ex)
        {
                throw ex;
        }
        return null;
    }

您可以请建议如何做到这一点?

Can you please suggest how to do this?

推荐答案

我已经打了同样的问题,我没有找到一种方法,使用它来下载文件 $。阿贾克斯但是我发现一个优秀的JavaScript库,它提供了类似的行为:

I've hit the same problem and I didn't find a way to download file using $.ajax but I found an excellent JavaScript library that provides similar behavior:

https://github.com/johnculviner/jquery.fileDownload

您只需要调用是这样的:

You just need to invoke something like this:

$.fileDownload(path, {
    httpMethod: 'POST',
    data: JSON.stringify(searchViewModel),
    success: function (result) {
    },
    error: function (error) {
        $("#voidcontainer").html("Error");
    }
});

要记住,在控制器上创建的cookie。在的src /通用有适当措施过滤器,为您代劳。

And remember to create cookie in controller. In src/Common there is suitable action filter that do it for you.

这篇关于如何处理阿贾克斯职位的FileStream返回类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆