繁体   English   中英

将压缩的 PDF 文件内容从客户端发送到后端服务器

[英]Send compressed PDF file contents from client side to backend server

我正在使用ilovepdf压缩在客户端上传的 pdf 文件。 我得到压缩文件内容作为响应。

理想情况下,我想要的是-:

  • 在客户端制作一个文件(PDF),文件内容收到响应。
  • 将文档附加到文件上传 DOM 元素。
  • 将文件提交到后端。

但看起来禁止在客户端(浏览器)读取/写入包含内容的文件。 所以以上无法实现。 我还尝试将压缩文件内容附加到 DOM 元素并使用 formdata 提交到后端,但是,当我使用后端收到的内容保存文件时,我发现文件已损坏或无法打开。

下面的代码被触发从 ilovepdf 服务器下载并上传到我的服务器。

function downloadPDF() {
  $.ajax({
    type: 'GET',
    headers: {
      'Authorization': 'Bearer ' + token
    },
    url: "https://" + server + "/v1/download/" + task,
    success: function(response) {
      var form = new FormData();
      form.append("file", response);
      $.ajax({
        type: 'POST',
        url: "/properties/compressed",
        processData: false,
        contentType: false,
        data: form,
        success: function(response) {
          console.log("sent to server")
        }
      });
    }
  });
}

收到压缩文件响应

You can generate the PDF from your server by using wicked_pdf gem and encode the same in base64 return the base64 encoded string to client server and the on your client server you can use this code for download the PDF:

window.downloadPDF = function downloadPDF() {

    var dlnk = document.getElementById('dwnldLnk');
    dlnk.href = pdf; <this would be your return encoded string in response>

    dlnk.click();


    alert('toma');
}

暂无
暂无

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

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