【问题标题】:How to handle FileStream return type in .ajax post?如何处理 .ajax 帖子中的 FileStream 返回类型?
【发布时间】:2013-01-06 03:08:03
【问题描述】:

我通过以下代码发送 JSON 对象。控制器以 CSV 格式返回值,应提供提示以打开或保存为 CSV 文件。我无法理解究竟应该写什么代码才能成功:函数(结果)

导出链接

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

}

控制器动作结果

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

你能建议怎么做吗?

【问题讨论】:

    标签: ajax asp.net-mvc actionlink actionresult


    【解决方案1】:

    我遇到了同样的问题,但我没有找到使用 $.ajax 下载文件的方法,但我找到了一个提供类似行为的优秀 JavaScript 库:

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

    你只需要调用这样的东西:

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

    记得在控制器中创建 cookie。在src/Common 中,有合适的操作过滤器可以为您完成。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多