簡體   English   中英

如何使用 angularjs 發布 blob 並在 C# 中接收 blob 到電子郵件

[英]How to POST blob using angularjs and receive blob in C# to Email

我想將 html2canvas 捕獲的圖像發布到我的 c# 控制器,接收它並將其插入電子郵件正文中,准備發送。

我正在嘗試使用 angularjs 發布一個從 html2canvas toDataURL() 函數返回的 base64 轉換的 blob。 我相信我應該將它作為 FormData() 發布,以便在 c# 中我可以接收它並將其重建為圖像以顯示在電子郵件正文中。

之后,它建議將 base64 轉換為 blob,但在 c# 中“body”被接收為“null”。 收件人和主題已正確填充,但只有正文被接收為“空”。 我試圖傳遞一個 base64 字符串,它解釋了我的控制器中的getEmbeddedImage函數。 我想嘗試使用 FormData() 但我找不到任何信息來接收 FormData() 並構建要顯示給用戶的 blob。

角度:

        html2canvas($('#quoteTable')[0], {
        letterRendering: 1,
        allowTaint: true,
        width: 1600,
        height: 1800
    }).then(function (canvas) {
        img = canvas.toDataURL();

        var tempImg = img;
        var base64ImageContent = tempImg.replace(/^data:image\/(png|jpg);base64,/, "");
        var blob = $scope.base64ToBlob(base64ImageContent, 'image/png');
        //var formData = new FormData();
        //formData.append('picture', blob);
        var data = {

            recipientEmail: "sample@sample.co.uk",

            subject: "test mail",

            body: blob

        };
        $http.post('/Home/EmailQuote', JSON.stringify(data)).then(function (response) {

            if (response.data)

                $scope.msg = "Post Data Submitted Successfully!";

        }, function (response) {

            $scope.msg = "Service not Exists";

            $scope.statusval = response.status;

            $scope.statustext = response.statusText;

            $scope.headers = response.headers();

        });
        var win = window.open();
        win.document.open();

        win.document.close();
    })
        .catch(function (error) {
            /* This is fired when the promise executes without the DOM */
            alert("could not generate canvas");
        });

在我的控制器中,我不確定要為重載“主體”放置什么類型以及如何在 angularjs 端傳遞它:

  [HttpPost]
    public void EmailQuote(string recipientEmail, string subject, string body)
    {
        SmtpClient client = new SmtpClient();
        client.Port = 587;
        client.Host = "smtp.gmail.com";
        client.EnableSsl = true;
        client.Timeout = 10000;
        client.DeliveryMethod = SmtpDeliveryMethod.Network;
        client.UseDefaultCredentials = false;
        client.Credentials = new System.Net.NetworkCredential("sample@gmail.com", "password");

        MailMessage mm = new MailMessage();
        mm.From = new MailAddress("sample@sample.co.uk");
        mm.To.Add(recipientEmail);
        mm.Subject = subject;
        mm.IsBodyHtml = true;

        mm.AlternateViews.Add(getEmbeddedImage(body));
        mm.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;


        try
        {
            client.Send(mm);
            ViewBag.MyProperty = "Successfully sent email";
        }
        catch (SmtpException ex)
        {
            ViewBag.Message = "Exception caught: " + ex;
        }


    }

    private AlternateView getEmbeddedImage(String filePath)
    {
        LinkedResource res = new LinkedResource(filePath);
        res.ContentId = Guid.NewGuid().ToString();
        string htmlBody = @"<img src='cid:" + res.ContentId + @"'/>";
        AlternateView alternateView = AlternateView.CreateAlternateViewFromString(htmlBody, null, MediaTypeNames.Text.Html);
        alternateView.LinkedResources.Add(res);
        return alternateView;
    }

我看過這個: How to read FormData C#但是,在重建 blob 時我不清楚,我是否需要一個 blob 構造函數的庫並通過 FormData 的內容設置它的每個屬性然后顯示體內的數據?

我發現使用 ajax 可以發送 base64,這就是我所做的!

        img = canvas.toDataURL();

        var data = {

            recipientEmail: "sample@sample.com",

            subject: "test mail",

            body: img

        };
        $.ajax({
            url: '/Home/EmailQuote',
            type: "POST",
            dataType: 'json',
            data: data,
            success: function (response) {
                if (response.success) {
                    alert('Email sent!');
                }
            }
        });

在 C# 方面,我使用了相同的舊代碼,但使用了下面的代碼而不是 mm.AlternateViews.Add(getEmbeddedImage(body)); 線。

mm.Body = "<img src='" + body + "' alt='image'/>";

使用模型

public class Model
{
    public string RecipientEmail { get; set; }
    public string Subject { get; set; }
    public string Body { get; set; }
}

[HttpPost]
public void EmailQuote(Model model)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM