如何处理阿贾克斯职位的FileStream返回类型?如何处理、职位、类型、阿贾克斯

由网友(老婆,我不想带套。)分享简介:我通过以下code发送的JSON对象。在CSV格式的控制器返回的值应该是提供PROMT打开或保存为CSV文件。我无法明白,究竟是什么code应该写入成功:函数(结果) I am sending JSON object through following code.. Controller returning valu...

我通过以下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.

阅读全文

相关推荐

最新文章