-1
我正在第一次使用angularjs。沒有太多的知識。 我正在使用模塊,我必須下載並上傳PDF格式的特定DIV。我能夠將DIV作爲圖像下載,但不知道如何將其上傳到服務器。下面是我的代碼。對於角度和PHP腳本都是。如何使用angularjs將圖像上傳到服務器?
$scope.createPdf = function() {
html2canvas(document.getElementById('exportthis'), {
onrendered: function (canvas) {
var data = canvas.toDataURL('image/png');
var docDefinition = {
content: [{image: data,width: 500}]
};
pdfMake.createPdf(docDefinition).download("offerLetter.pdf");
}
});
$.ajax({
type: "POST",
url: "demo_angularjs.php",
data: {
imgBase64: toDataURL
}
}).done(function() {
console.log('saved');
});
};
<?php
define('UPLOAD_DIR', 'uploads/');
$img = $_POST['exportthis'];
$img = str_replace('data:image/png;base64,', '', $img);
$img = str_replace(' ', '+', $img);
$data = base64_decode($img);
$file = UPLOAD_DIR . uniqid() . '.png';
$success = file_put_contents($file, $data);
print $success ? $file : 'Unable to save the file.';
?>
我建議有一看[NG-文件上傳(https://github.com/danialfarid/angular-file-upload) –
我需要上傳DIV作爲PDF onclick按鈕我不想上傳文件@GangadharJannu –
最後,無論如何這是一個二進制文件。所以你可以發送它像任何其他文件 –