【问题标题】:Trouble sending an generated jsPdf to server无法将生成的 jsPdf 发送到服务器
【发布时间】:2019-02-05 08:20:48
【问题描述】:

我使用 jsPdfHtml2Canvas 生成了一个 PDF。它工作得很好,并且可以下载。

我现在的目标是将生成的.pdf 保存到我的服务器,这样我就可以通过 phpmailer 将其发送出去。这就是我处理这个问题的方式。

function print() {
    document.getElementById("out").textContent = document.getElementById("fader").value;
    const filename = 'DHC_Herren_Front.pdf';

    html2canvas(document.querySelector('#pdf')).then(canvas => {
        let pdf = new jsPDF('l', 'mm', 'a4');
        pdf.addImage(canvas.toDataURL('image/png'), 'PNG', 0, 0, 298, 211, function () {
            var blob = doc.output('blob');

            var formData = new FormData();
            formData.append('pdf', blob);

            $.ajax('/st/tda/dhc/men_front/upload.php', {
                method: 'POST',
                data: formData,
                processData: false,
                contentType: false,
                success: function (data) {
                    console.log(data)
                },
                error: function (data) {
                    console.log(data)
                }
            });
        });

    });
}

还有我的upload.php

<?php move_uploaded_file(
    $_FILES['pdf']['tmp_name'],
    $_SERVER['DOCUMENT_ROOT'] . "/st/tda/dhc/men_front/test.pdf");
?>

我的问题是,为什么我最终在服务器上没有文件。我觉得必须有一个简单的解决方案,但我无法确定它。

最新的 HTML

       function ma() {
       document.getElementById("out").textContent = document.getElementById("fader").value;


            html2canvas(document.querySelector('#pdf')).then(canvas => {
                    var pdf = btoa(doc.output());
                    pdf.addImage(canvas.toDataURL('image/png'), 'PNG', 0, 0, 298, 211,);


$.ajax({
  method: "POST",
  url: "/st/tda/dhc/men_front/upload.php",
  data: {data: pdf},
}).done(function(data){
   console.log(data);
});

            });


 }

最新上传.php

   <?php
   if(!empty($_POST['data'])){
    $data = base64_decode($_POST['data']);
    // print_r($data);
   file_put_contents( "test.pdf", $data );
   } else {
   echo "No Data Sent";
   }
   exit();

【问题讨论】:

  • 您是否收到脚本输出的任何错误消息?您检查过网络服务器的错误日志吗?
  • Phpmailer 从何而来?
  • 从哪里调用move_uploaded_file()。我怀疑权限错误。
  • move_uploaded_file(),至少到目前为止(再次 Newb ^^)只需在新选项卡中打开 upload.php 即可调用。 (没有复制它,因为它似乎不重要。)我重新检查了我文件夹的权限。它应该被允许写在那里。 ://
  • 一旦我到达那里,我将穿过 phpmailer 桥。现在我想我应该处理更紧迫的事情,保存一些东西,我可以稍后将其附加到服务器上。

标签: javascript php jspdf


【解决方案1】:

这就是我的做法。看一下,看看您是否可以将其调整为您的代码。这是 ajax 将文件发送到的 uploadFiles.php。

<?php
$ds = DIRECTORY_SEPARATOR; // a directory separator
$cid = $_POST["cid"]; // directory name passed from the form as a variable
$rid = $_POST["rid"]; // directory name passed from the form as a variable

$storeFolder = "../recordFiles/".$cid."/".$rid; // the place where i want to save stuff

// run only if there are files sent from ajax.
if (!empty($_FILES)) {
	
    // check if the directory exists and if not then create it.
	if (!file_exists('../recordFiles/'.$cid."/".$rid)) {
		mkdir('../recordFiles/'.$cid."/".$rid, 0777, true);
	}
	
    // get the temp file name
    $tempFile = $_FILES['file']['tmp_name'];
	
	// remove all whitespace in the file name
	$cleanedFileName =  $_FILES['file']['name'];
	$cleanedFileName = preg_replace('/\s+/', '', $cleanedFileName);
	
    // set the target path
    $targetPath = dirname( __FILE__ ).$ds.$storeFolder.$ds;
     
    // set the target file name and path
    $targetFile =  $targetPath.$cleanedFileName;
 
    // move the files
    move_uploaded_file($tempFile,$targetFile);
   
}

?>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多