I want to upload a pdf from jspdf to my server. I use Ajax for it.
I open the Javascript by a function in my form.
onsubmit="pdferzeugen();"
above the ajax code i create the pdf by "jspdf" and save it to a datauri (base64).
Javascript:
var datauri = pdf.output('datauri');
var data = new FormData();
data.append("pdf_data", datauri);
$.ajax({
url: "./uploads/upload.php",
type: "POST",
processData: false,
contentType: false,
data: data,
});
upload.php:
if(!empty($_POST['pdf_data'])){
$data = $_POST['pdf_data'];
$fname = "test.pdf"; // name the file
$file = fopen("./uploads/" .$fname, 'w'); // open the file path
fwrite($file, $data); //save data
fclose($file);
}
But this isnt working for me. Chrome always say:
jquery.min.js:9600 XHR failed loading: POST "http://app-her-visitor/begehung/uploads/upload.php".
I searched whole google for this error, but no success. I hope you can help me:-)
Thanks for help
Greetings
The issue is between the data you are sending. Verify your data before sending it through ajax because the data type should be:
Good Luck.
The answer was i refreshed the page after submitting the form and send the base64 Data to my server.
I need to prevent it from refreshing page so it will successfully send the data.
HTML
onsubmit="pdferzeugen();return false"
Javascript
var datauri = pdf.output('dataurl');
$.ajax({
url: "./uploads/upload.php",
type: "POST",
data: { pdf: datauri },
success: function(data) {},
});
PHP
$pdf = str_replace('data:application/pdf;base64,', '', $_POST['pdf']);
$pdf_decoded = base64_decode ($pdf);
$pdf = fopen ('Begehungsprotokoll.pdf','w');
fwrite ($pdf,$pdf_decoded);
fclose ($pdf);
The return false
in the onsubmit will prevent it from reload the page and now the pdf upload to my server.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.