首页 > 解决方案 > 如何将 FormData 对象中的文件从 AngularJS 发布到 ASP.NET

问题描述

我正在尝试使用 HTML2CANVAS 将 html 中的发票页面截图为画布,然后使用 pdfmake 创建一个 pdf,我需要通过 http post 将 pdf 文件发送到后端,我有这个功能:

 $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 () {
                 }
            );              
        });
    });
};

我尝试了 FormData 并单独发送 Blob,但它在后端显示为 null。

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

    }

标签: .netangularjshtml2canvasform-datapdfmake

解决方案


推荐阅读