【问题标题】:How to show a PDF file in a new browser window with ajax POST and ASP.net MVC FileStreamResult?如何使用 ajax POST 和 ASP.net MVC FileStreamResult 在新的浏览器窗口中显示 PDF 文件?
【发布时间】:2016-08-03 20:46:55
【问题描述】:

我有一个控制器动作:

public ActionResult GetReport(List<int> idList)
{
    Stream stream = ReportsComponent.GetReports(idList);
    return File(stream, "application/pdf");
}

要访问我有 javascript:

$.ajax({
    method: "POST",
    url: "/Reports/GetReport",
    data: { idList : idsList}
})
.then(function(data){
        /*Not working*/
        var blob=new Blob([data], {type: "application/pdf"});

        var fileURL = URL.createObjectURL(blob);
        window.open(fileURL);
        /*Not working*/
    },
    function(){
        //error
    }
);

到目前为止,我收到了(使用无效代码)损坏的 PDF。如何在新的浏览器窗口中显示生成的 PDF?

【问题讨论】:

  • 您无法使用 ajax 下载文件。您需要重定向到返回文件的方法。
  • 您是否必须发出 POST 请求?如果您将其更改为 GET 请求,那么您就不必担心 ajax。

标签: jquery asp.net ajax asp.net-mvc blob


【解决方案1】:

我只需要尝试this(所以我不是原来的):

  • 注意small PDF only,你可以查看关于插件的其他答案(我没有测试过所说的插件)。我可以确认,在大型 PDF 上,这将不起作用

  • 我使用 24k PDF 文件进行了测试(仅限本地开发盒 - 说明因为不确定现实世界中的网络延迟会产生什么影响)

  • Chrome 中的弹出窗口将被阻止

Home控制器:

public ContentResult ReturnPdf()
{
    var file = System.IO.File.ReadAllBytes(Server.MapPath("~/content/pdf/foopdf.pdf"));
    return Content(Convert.ToBase64String(file));
}

查看:

<div>
    <button id="get-pdf">Download PDF</button>
</div>

@section scripts{
   <script>
       $(function () {

           $("#get-pdf").click(function () {

               $.get("@Url.Action("ReturnPdf", "Home")").success(function (d) {
                   window.open("data:application/pdf;base64," + d);
               })

           });

       });
    </script>
}

第...

【讨论】:

    【解决方案2】:

    你的控制器应该是这样的,

     public FileResult GetReport(string Path)
     {
            try
            {
                if (!string.IsNullOrEmpty(Path))
                {
                    var file = new FileInfo(Path);
                    if (file.Exists)
                    {
                        return new FilePathResult(Path, "application/pdf");
                    }
                }
    
                //file is empty, so return null
                return null;
            }
            catch (Exception ex)
            {
                Utility.LogError(ex);
                throw;
            }
        }
    

    这里我传递 Path 作为参数,但你可以传递 idList 或任何字符串。

     $.ajax({
                url: "@Url.Content("~/Reports/GetReport")",
                type: "Post",
                data: {"Path":"\\F:MyPDF.pdf"}
                beforeSend: function () { },
                success: function (result) {
                    if (result.IsSuccess == false) {
                        alert(result.ErrorMessage)
                    }
                    else {
                        $('#divAlert').html('')
                        $('#divAlert').append(result)
                    }
                }
            })
            return false;
    

    这只是为了你的演示,

    这些代码在我的最后工作正常,但如果您有任何疑问,请发表评论。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-03-24
      • 1970-01-01
      • 2016-08-03
      • 1970-01-01
      • 1970-01-01
      • 2022-12-08
      • 2016-11-05
      相关资源
      最近更新 更多