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
2
Answers
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
Javascript
PHP
The
return false
in the onsubmit will prevent it from reload the page and now the pdf upload to my server.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.