0

I'm trying to screenshot an invoice page in html as canvas with HTML2CANVAS, then create a pdf with pdfmake, I need to send the pdf file to the backend via http post, I have this function :

 $scope.createPdf = function () {
    html2canvas(document.getElementById('exportthis')).then(canvas => {
        var data = canvas.toDataURL();
        var docDefinition = {
            content: [{
                image: data,
                width: 500,
            }]
        };
        var file = pdfMake.createPdf(docDefinition)
        var Fileblob = file.getBlob((Blob) => {
            Blob.lastModifiedDate = new Date();
            Blob.name = "Invoice.pdf;"
            console.log(Blob);
            var fd = new FormData();
            fd.append('file', Blob, 'Invoice.pdf');
            $http.post("/Home/SendInvoice", {data: fd})
                 .then(function successCallback(data) { }, 
                       function errorCallback(response) {
                             console.log(response.statusText);
                       })
                 .finally(function () {
                 }
            );              
        });
    });
};

I tried FormData and sending the Blob alone but it shows null in the backend.

 public JsonResult SendInvoice(HttpPostedFileBase Attachement)
    {
        //Send Email with the file as an attachement
        *
        *
        return Json("Email Sent", JsonRequestBehavior.AllowGet); ;

    }
Alfredo A.
  • 1,697
  • 3
  • 30
  • 43
Ayoub Salhi
  • 312
  • 1
  • 4
  • 19
  • Try to look at the controller inside `Controller.Request.Files` if your problem that you do not receive files. If you do receive pdf then history is different – Vladimir Oct 25 '18 at 15:25
  • Request.Files.Count is returning 0, no files received. – Ayoub Salhi Oct 25 '18 at 15:42
  • try to add console.log inside js - `pdfMake.createPdf(docDefinition)`. If it is create a file then you should revise sending code in js part. – Vladimir Oct 25 '18 at 15:46
  • When posting objects created by the [FormData API](https://developer.mozilla.org/en-US/docs/Web/API/FormData), it is important to set the `Content-type` header to `undefined`. – georgeawg Oct 25 '18 at 15:55
  • I was able to achieve this by using `ngFileUpload` that is used to upload files. I replaced the http post with the following and it works : file.upload = Upload.upload({ url: '/Home/SendInvoice', data: { Attachment: files }, }); – Ayoub Salhi Oct 25 '18 at 19:32

0 Answers0