简体   繁体   English

在JavaScript中使用https路径下载pdf文件

[英]Download a pdf file using https path in javascript

I have an ajax call to the controller which returns in success block as a path to the pdf file which something looks like this www. bts.abcd.com/ReportPdf/BH00118051710501_1_1.pdf 我对控制器进行了ajax调用,该调用以成功的块形式返回到pdf文件的路径,该文件看起来像是www. bts.abcd.com/ReportPdf/BH00118051710501_1_1.pdf www. bts.abcd.com/ReportPdf/BH00118051710501_1_1.pdf , now i want to write some code in javascript that would download this file. www. bts.abcd.com/ReportPdf/BH00118051710501_1_1.pdf ,现在我想用javascript编写一些代码来下载此文件。 I have tried various methods like winodw.location.href="path" but this only opens the file in new window not downloading it. 我尝试了各种方法,例如winodw.location.href="path"但这只会在新窗口中打开文件而不下载文件。 here's my code. 这是我的代码。

` `

$.ajax({
      type: "POST",
     url: "/Reporting/ReportAPI",
     data: '{PatientId:"BH0012"}',
    contentType: "application/json,utf=charset-8",
    datatype: "JSON",
     success: function (response) {
//response= www.bts.abcd.com/ReportPdf/BH00118051710501_1_1.pdf
//Code for downloading the file
                }
            });

` Different methods to achieve the same result will also be appreciated. `也将了解获得相同结果的不同方法。

One of the ways that were already mentioned would be by appending an invisible <iframe> and appending a URL in JS but there will be some security errors that will creep up. 已经提到的方法之一是通过在JS中添加一个不可见的<iframe>并添加一个URL ,但是将会出现一些安全错误。


In my method below, I have used Axios.js to allow me to safely download the file which also includes Promises . 在下面的方法中,我使用了Axios.js来允许我安全地下载还包含Promises的文件。


Axios.JS CDN: <script src="https://cdnjs.cloudflare.com/ajax/libs/axios/0.18.0/axios.js"></script> Axios.JS CDN: <script src="https://cdnjs.cloudflare.com/ajax/libs/axios/0.18.0/axios.js"></script>

JS JS


function DownloadFromUrl(url, mime) {
  axios({
    url: url,
    method: 'GET',
    responseType: 'blob', // important
  }).then((response) => {
    const url = window.URL.createObjectURL(new Blob([response.data]));
    const link = document.createElement('a');
    link.href = url;
    link.setAttribute('download', 'file.' + mime);
    document.body.appendChild(link);
    link.click();
  });
}

$.ajax({
  type: "POST",
  url: "/Reporting/ReportAPI",
  data: '{PatientId:"BH0012"}',
  contentType: "application/json,utf=charset-8",
  datatype: "JSON",
  success: function(response) {
    // Response = URL
    // Mime     = File Type - pdf, jpeg, png, bmp, exe, js...
    DownloadFromUrl(response, "pdf");
  }
});

Make sure to wrap the MIME parameter as a string, or change the code above to validate the MIME Type 确保将MIME参数包装为字符串,或更改上面的代码以验证MIME Type

Here's JSFiddle: https://jsfiddle.net/5mnethL1/2/ 这是JSFiddle: https ://jsfiddle.net/5mnethL1/2/

"winodw.location.href" cannot work in the ajax block. “ winodw.location.href”不能在ajax块中工作。 you can create a element, then click this. 您可以创建一个元素,然后单击它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM