从Base-64字符串转换为图像时异常。

时间:2022-11-13 12:26:30

I am trying to send a Highcharts chart via image on ASP.NET button click. What I am trying to do is:

我正在尝试通过ASP上的图像发送一个Highcharts图表。净按钮点击。我想做的是:

Convert the chart to base64 image, the code is the following :

将图表转换为base64图像,代码如下:

  var chart = $('#main-content').highcharts();
    EXPORT_WIDTH = 1000;
    var render_width = EXPORT_WIDTH;
    var render_height = render_width * chart.chartHeight / chart.chartWidth;

    var svg = chart.getSVG({
        exporting: {
            sourceWidth: chart.chartWidth,
            sourceHeight: chart.chartHeight
        }
    });
    var contentToSend = 'data:image/svg+xml;base64,' + window.btoa(svg);
    var hdnField = document.getElementById("MainContent_ChartImage");
    hdnField.value = contentToSend;

Next step is taking the base64 image value, convert it to image an attach it to the mail, the code is:

下一步是取base64图像值,将其转换为图像,并将其附加到邮件中,代码为:

 string textImage = ChartImage.Value;

 var imageData = Convert.FromBase64String(HttpUtility.UrlDecode(data));
 System.Net.Mail.LinkedResource res;
 AlternateView htmlView;
 using (MemoryStream ms = new MemoryStream(imageData, true))
 {
      ms.Position = 0;
      ms.Write(imageData, 0, imageData.Length);
      ms.Seek(0, SeekOrigin.Begin);
      res = new System.Net.Mail.LinkedResource(ms);
      htmlView = AlternateView.CreateAlternateViewFromString("<html><body><img src='cid:imageReport' width='100%' ></body></html>", null, "text/html");
      res.ContentId = "imageReport";
      htmlView.LinkedResources.Add(res);
      MailMessage mailMsg = new MailMessage();
      SmtpClient client = new SmtpClient();

      // ...

      mailMsg.IsBodyHtml = true;
      mailMsg.AlternateViews.Add(htmlView);
      client.Send(mailMsg);
 }

but the method Convert.FromBase64String throws an exception

但是转换的方法。FromBase64String抛出一个异常

{"The input is not a valid Base-64 string as it contains a non-base 64 character, more than two padding characters, or an illegal character among the padding characters. "}

{"该输入不是一个有效的Base-64字符串,因为它包含一个非base 64字符、两个以上的填充字符或填充字符之间的非法字符。" }

However when I remove 'data:image/svg+xml;base64,' then convert it, it doesn't throw an exception but the image will not appear. What should I do?

但是,当我删除“数据:图像/svg+xml;base64”时,它不会抛出异常,但图像不会出现。我应该做什么?

Thank you

谢谢你!

2 个解决方案

#1


1  

Get rid of the beginning part of the string: "data:image/svg+xml;base64," that part is not base64, just the remainder is. You don't need to use HttpUtility.UrlDecode either.

去掉字符串的开头部分:“data:image/svg+xml;base64”,该部分不是base64,其余部分是。您不需要使用HttpUtility。UrlDecode。

You should specify the TransferEncoding as Base64:

你应指定传送编码为Base64:

res.TransferEncoding = System.Net.Mime.TransferEncoding.Base64;

However with all that said, there are some strong caveats to using SVG in email. So you may want to consider a different format such as JPG or PNG. If that's the route you take, you will need to use a library to convert formats.

然而,尽管如此,在电子邮件中使用SVG还是有一些强烈的警告。因此,您可能需要考虑一种不同的格式,如JPG或PNG。如果您采用的是这种方式,您将需要使用库来转换格式。

#2


0  

After many researches I found the solution , the main problem was that not all client email support data URI : What is Data URI support like in major email client software?

经过很多研究,我找到了解决方案,主要的问题是并不是所有的客户端电子邮件支持数据URI:在主要的电子邮件客户端软件中,什么是数据URI支持?

i was trying to open the mail from the outlook 2016 however it is not supported , when i opened from hotmail.com it worked..

我试着从2016年的outlook上打开邮件,但是它不受支持,当我从hotmail.com打开它的时候。

the code is :

的代码是:

MailMessage mailMsg = new MailMessage();
SmtpClient client = new SmtpClient();
var imageData = Convert.FromBase64String(data);
var contentId = Guid.NewGuid().ToString();
var linkedResource = new LinkedResource(new MemoryStream(imageData), "image/svg+xml");
linkedResource.ContentId = contentId;
linkedResource.TransferEncoding = TransferEncoding.Base64;
var body = string.Format("<img src=\"cid:{0}\" />", contentId);
var htmlView = AlternateView.CreateAlternateViewFromString(body, null, "text/html");
 htmlView.LinkedResources.Add(linkedResource);
 mailMsg.AlternateViews.Add(htmlView);

#1


1  

Get rid of the beginning part of the string: "data:image/svg+xml;base64," that part is not base64, just the remainder is. You don't need to use HttpUtility.UrlDecode either.

去掉字符串的开头部分:“data:image/svg+xml;base64”,该部分不是base64,其余部分是。您不需要使用HttpUtility。UrlDecode。

You should specify the TransferEncoding as Base64:

你应指定传送编码为Base64:

res.TransferEncoding = System.Net.Mime.TransferEncoding.Base64;

However with all that said, there are some strong caveats to using SVG in email. So you may want to consider a different format such as JPG or PNG. If that's the route you take, you will need to use a library to convert formats.

然而,尽管如此,在电子邮件中使用SVG还是有一些强烈的警告。因此,您可能需要考虑一种不同的格式,如JPG或PNG。如果您采用的是这种方式,您将需要使用库来转换格式。

#2


0  

After many researches I found the solution , the main problem was that not all client email support data URI : What is Data URI support like in major email client software?

经过很多研究,我找到了解决方案,主要的问题是并不是所有的客户端电子邮件支持数据URI:在主要的电子邮件客户端软件中,什么是数据URI支持?

i was trying to open the mail from the outlook 2016 however it is not supported , when i opened from hotmail.com it worked..

我试着从2016年的outlook上打开邮件,但是它不受支持,当我从hotmail.com打开它的时候。

the code is :

的代码是:

MailMessage mailMsg = new MailMessage();
SmtpClient client = new SmtpClient();
var imageData = Convert.FromBase64String(data);
var contentId = Guid.NewGuid().ToString();
var linkedResource = new LinkedResource(new MemoryStream(imageData), "image/svg+xml");
linkedResource.ContentId = contentId;
linkedResource.TransferEncoding = TransferEncoding.Base64;
var body = string.Format("<img src=\"cid:{0}\" />", contentId);
var htmlView = AlternateView.CreateAlternateViewFromString(body, null, "text/html");
 htmlView.LinkedResources.Add(linkedResource);
 mailMsg.AlternateViews.Add(htmlView);