【问题标题】:Pdf document as response for ajax callPdf 文档作为 ajax 调用的响应
【发布时间】:2019-11-29 08:01:24
【问题描述】:

我需要一个脚本,将 json 字符串传递给控制器​​中的函数并处理响应 pdf 文档,以将其视为下载给用户。

我在 c# 中返回一个 FileResult 数据类型的 pdf 文档作为 ajax 调用的响应,该调用将 JSON 字符串传递给函数。 我需要将此 pdf 文档视为下载给用户。我该怎么做?我尝试在ajax成功的情况下编写代码,但是控件出错,通过调试我发现它是一个解析错误。

Ajax 代码就像

function Download(){
    $.ajax({
        method: "POST",
        url: "{url to the action}",
        data: {
            //Send json data that you want
        }
        success:function (e){
            alert('success');
        },
        error:function(){
            alert("error");
        }
    })

}

C#代码如下:

public FileResult getPDF(string statementHTML)
{ 

//code to convert to pdf file
File file = //pdf document with MIME 'apllication/pdf'
return file;
}

当上面的代码执行时,会调用错误警报,但是这个 c# 函数返回一个 pdf 文档,但似乎 ajax 不接受它。

【问题讨论】:

  • 显示您的代码,也许我们可以帮助您
  • @MarkSchultheiss 我已经添加了。

标签: c# ajax pdf asp.net-core model-view-controller


【解决方案1】:

您实际上不需要 ajax 来进行此下载调用,您可以这样做。

function DownloadFile(){
    //do some calculation
    window.location="{url to the action}?argument1=value1&argument2=value2....."
}

但是,如果您必须通过 ajax 执行此操作,您可以获取 download.js 并执行此操作。

function DownloadFileUsingAjax(){
    $.ajax({
        url: "{url to the action}",
        data: {
            //Send json data that you want
        }
    })
    .done(function(data){
        //download function is from download.js
        download(data, fileName, fileMimeType);
    });
}

【讨论】:

    【解决方案2】:

    对于另一种方式,您可以尝试模拟按钮单击事件来下载文件,如

    客户端。

    @section Scripts{ 
        <script type="text/javascript">
            $(document).ready(function(){
                $.ajax({
                    url: '/Home/GetPDF',
                    method: 'get',
                    contentType:'application/json',
                    xhrFields: {
                        responseType: 'blob'
                    },
                    success: function (data, status, response) {
                        var filename = "";
                        var disposition = response.getResponseHeader('Content-Disposition');
                        if (disposition && disposition.indexOf('attachment') !== -1) {
                            var filenameRegex = /filename[^;=\n]*=((['"]).*?\2|[^;\n]*)/;
                            var matches = filenameRegex.exec(disposition);
                            if (matches != null && matches[1]) filename = matches[1].replace(/['"]/g, '');
                        }
                        let a = document.createElement('a');
                        a.href = window.URL.createObjectURL(data);
                        a.download = filename;
                        document.body.append(a);
                        a.click();
                        window.URL.revokeObjectURL(url);
                    }
                });
            });
        </script>
    }
    

    控制器

    public FileResult GetPDF(string statementHTML)
    {
        var fileByte = System.IO.File.ReadAllBytes(@"xxx\Test.pdf");
        return File(fileByte, "apllication/pdf", "Test.pdf");
    }
    

    【讨论】:

      猜你喜欢
      • 2012-06-09
      • 1970-01-01
      • 2018-01-31
      • 1970-01-01
      • 1970-01-01
      • 2016-11-26
      • 1970-01-01
      • 1970-01-01
      • 2011-03-18
      相关资源
      最近更新 更多