简体   繁体   English

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

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

I'm using ilovepdf to compress a pdf file uploaded at the client side.我正在使用ilovepdf压缩在客户端上传的 pdf 文件。 I get the compressed file contents as the response.我得到压缩文件内容作为响应。

Ideally what I want is to -:理想情况下,我想要的是-:

  • Make a file(PDF) at the client side with file contents received response.在客户端制作一个文件(PDF),文件内容收到响应。
  • Attach the document to the file upload DOM element.将文档附加到文件上传 DOM 元素。
  • Submit the file to the backend.将文件提交到后端。

But it looks like reading/writing a file with contents at client side(browser) is forbidden.但看起来禁止在客户端(浏览器)读取/写入包含内容的文件。 So above could not be acheived.所以以上无法实现。 I also tried attaching just compressed file contents to a DOM element and submitting to backend with formdata, however, when i save file with contents received at backend, I find the file as damaged or cannot open.我还尝试将压缩文件内容附加到 DOM 元素并使用 formdata 提交到后端,但是,当我使用后端收到的内容保存文件时,我发现文件已损坏或无法打开。

Below code is triggered for download from ilovepdf server and upload to my server.下面的代码被触发从 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: 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.

相关问题 从服务器向客户端发送 PDF - Send PDF from Server to Client PDF.JS从服务器端发送的客户端上预览大型pdf文件(&gt; 200MB) - PDF.JS Preview large pdf file (> 200MB) on client side, that was sent from server side 使用 JavaScript 从客户端上传压缩图像文件 - Upload compressed image file from client-side using JavaScript 如何将 javascript 文件(其中包含多个函数)从服务器发送到客户端? - How to send a javascript file (with several functions in it) from the server to the client side? 无法从客户端将文件数据发送到Node.js服务器 - Unable to send file data to nodejs server from client side 在客户端PC上下载pdf文件,并在开始下载后从服务器端文件夹中删除pdf文件 - download pdf file at client pc and remove pdf file from server side folder after download begin 从客户端发送数据到服务器端 - Send data to server side from client 如何在不使用处理程序的情况下使用ajax从客户端将文件发送到服务器端? - How to send file to server side from client side using ajax without using handler? 如何将jsPDF转换的pdf文件发送到后端服务器? - How to send a jsPDF converted pdf file to backend server? 在React(客户端)中上传.json文件并将其发送到Node(服务器端) - Upload .json file in React(client side) and send it to Node(server side)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM