【问题标题】:How to use file response in Ajax query如何在 Ajax 查询中使用文件响应
【发布时间】:2022-11-16 15:37:51
【问题描述】:
//returning File Response in Controller action Method 
   
return File(MyMemoryStream.ToArray(), "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "lp.xlsx");

我想下载此文件,但我使用 ajax 将数据从视图传递到控制器,因此它返回到 Ajax 成功函数。

<script>
        $(".pfamlink").click(function()
        {
             $.ajax({
                      type: "GET",
                       url: "/Home/Excel",
                      data: { "data":$(this).html()},
                     
            success: function(response)
            {}
                   
        });
        });
        </script>

我需要在此函数(响应)中提供什么才能从控制器下载返回文件?

或在没有 Ajax 的情况下将表数据从视图传递到控制器的任何建议?

【问题讨论】:

    标签: javascript html jquery asp.net-mvc asp.net-core


    【解决方案1】:

    我需要在此函数(响应)中提供什么以下载返回 来自控制器的文件?

    FileContentResult返回给Ajax成功函数,下载不成功,因为我们需要操作成功。尝试添加一个下载动作,使用window.location 重定向到控制器中的下载动作。

    public async Task<IActionResult> DownloadAsync()
            {
            ...
          //returning File Response in Controller action Method 
       
            return File(MyMemoryStream.ToArray(), "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "lp.xlsx");
    

    在您的 ajax 成功中,更改您的代码如下:

    success: function (data) {
                    window.location = '/yourcontrollername/Download';
                }
    

    【讨论】:

      猜你喜欢
      • 2015-01-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-27
      • 1970-01-01
      • 2013-10-07
      • 1970-01-01
      相关资源
      最近更新 更多