2015-06-01 37 views

回答

1

(未測試的)
PHP:

<? 
// pull the raw binary data from the POST array 
$data = substr($_POST['data'], strpos($_POST['data'], ",") + 1); 
// decode it 
$decodedData = base64_decode($data); 
// print out the raw data, 
echo ($decodedData); 
$filename = "test.pdf"; 
// write the data out to the file 
$fp = fopen($filename, 'wb'); 
fwrite($fp, $decodedData); 
fclose($fp); 
?> 

JS:

var docDefinition = { 
    content: 'This is an sample PDF printed with pdfMake' 
}; 

pdfMake.createPdf(docDefinition).getBuffer(function(buffer) { 
    var blob = new Blob([buffer]); 

    var reader = new FileReader(); 
    // this function is triggered once a call to readAsDataURL returns 
    reader.onload = function(event) { 
    var fd = new FormData(); 
    fd.append('fname', 'test.pdf'); 
    fd.append('data', event.target.result); 
    $.ajax({ 
     type: 'POST', 
     url: 'upload.php', // Change to PHP filename 
     data: fd, 
     processData: false, 
     contentType: false 
    }).done(function(data) { 
     // print the output from the upload.php script 
     console.log(data); 
    }); 
    }; 
    // trigger the read from the reader... 
    reader.readAsDataURL(blob); 
}); 

上傳和從How can javascript upload a blob?接收代碼。

+0

你可以直接從pdfmake'pdfMake.createPdf(docDefinition).getBase64(function(dataURL){// do something}'獲得base64 –

相關問題