【问题标题】:Get FileStreamResult using ajax asp.net core使用 ajax asp.net core 获取 FileStreamResult
【发布时间】:2017-12-20 08:54:31
【问题描述】:

我在控制器中有一个名为 Export 的函数

    [HttpPost]
    [ActionName("Export")]
    public async Task<FileStreamResult> Export(int a)
    {
        var fileStream = new MemoryStream();
        var contentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";

        return new FileStreamResult(fileStream, contentType);
    }

我使用 ajax 从视图中调用了这个函数。

  $('#GetDetails').click(function () {

            $.ajax({
                url: '@Url.Action("Export", "ExportNow")',
                type: 'POST',
                data: {a:1},
                success: function (result) {
                    alert('Success.');
                },
                error: function (result) {
                    alert('Failed to Export.');
                }
            });
        });

如何打开从导出函数返回的 FileStreamResult(下载文件)。 请任何人帮忙。

【问题讨论】:

  • 和你要找的很相似:stackoverflow.com/questions/37462263/…
  • 他的是pdf格式,我需要的是xlsx格式,他的函数是返回一个文件,我正在返回一个文件流结果
  • 我曾尝试过该解决方案,但它不适用于我,我的函数正在接受一个参数。

标签: c# asp.net ajax filestream filestreamresult


【解决方案1】:

这对我有用:

$('#GetDetails').click(function () {
    window.open('controlleraction/document?id=xx', '_blank');
});

【讨论】:

  • 问题要求进行 ajax 调用
猜你喜欢
  • 2020-04-04
  • 1970-01-01
  • 2013-03-29
  • 1970-01-01
  • 1970-01-01
  • 2015-08-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多