【发布时间】:2019-03-08 17:48:25
【问题描述】:
我想将 html2canvas 捕获的图像发布到我的 c# 控制器,接收它并将其插入到电子邮件正文中,准备发送。
我正在尝试使用 angularjs 发布一个从 html2canvas toDataURL() 函数返回的 base64 转换而来的 blob。我相信我应该将它作为 FormData() 发布,以便在 c# 中我可以接收它并将其重建为图像以显示在电子邮件正文中。
在this 之后,它建议将 base64 转换为 blob,但在 c# 中“body”被接收为“null”。收件人和主题被正确填充,但只有正文被接收为“null”。我试图传递一个 base64 字符串,它解释了我的控制器中的 getEmbeddedImage 函数。我想尝试使用 FormData(),但找不到任何信息来接收 FormData() 并构建要显示给用户的 blob。
Angularjs:
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");
});
在我的控制器中,我不确定要为重载“body”放置什么类型,以及如何在 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 的内容设置它的每个属性,然后显示体内的数据?
【问题讨论】:
标签: c# angularjs html2canvas