Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将图像从字节附加到电子邮件_C#_Angularjs_Wcf_Rest_Bytearray - Fatal编程技术网

C# 将图像从字节附加到电子邮件

C# 将图像从字节附加到电子邮件,c#,angularjs,wcf,rest,bytearray,C#,Angularjs,Wcf,Rest,Bytearray,我正忙于了解如何处理网站->服务->电子邮件中的图像 到目前为止,我所做的是将图像转换为base64,然后将其上传到我的服务中。接下来,我将其转换为字节数组,然后将其存储到数据库中 在将字节插入数据库的同时,我希望通过将图像附加到电子邮件来发送图像 此时附件只是一个我无法打开的文件,所以我猜它只是一个字节数组,而不是一个图像 C# Angularjs $scope.link = '\img\ionic.png'; var imageData=$base64.encode($scope.link)

我正忙于了解如何处理
网站->服务->电子邮件中的图像

到目前为止,我所做的是将图像转换为
base64
,然后将其上传到我的服务中。接下来,我将其转换为字节数组,然后将其存储到数据库中

在将字节插入数据库的同时,我希望通过将图像附加到电子邮件来发送图像

此时附件只是一个我无法打开的文件,所以我猜它只是一个字节数组,而不是一个图像

C#

Angularjs

$scope.link = '\img\ionic.png';
var imageData=$base64.encode($scope.link);

  $scope.sendimg = function() {

    console.log(imageData);
    $http.post("http://localhost:53101/TruckService.svc/sendEmail/" + imageData + '|' + 1
    )
              .success(function(data) { 
 })

          .error(function(data) {
              console.log("failure");
              })    
  }

就我而言,它来自我的资源。但最终还是一样的。在生成一个字节[]后,我按如下方式附加它:

byte[] logo = BmpToBytes_MemStream(Properties.Resources.gcs_logo);
emailMessage.Attachments.AddFileAttachment("logo.jpg", logo);
emailMessage.Attachments[emailMessage.Attachments.Count - 1].IsInline = true;
emailMessage.Attachments[emailMessage.Attachments.Count - 1].ContentId = "gcs_logo.jpg";

private static byte[] BmpToBytes_MemStream(Bitmap bmp)
{
    byte[] bmpBytes = null;
    using (MemoryStream ms = new MemoryStream())
    {
        // Save to memory using the Jpeg format
        bmp.Save(ms, ImageFormat.Jpeg);

        // read to end
        bmpBytes = ms.GetBuffer();
        bmp.Dispose();
    }

    return bmpBytes;
}

希望这有帮助。

在我的情况下,它来自我的资源。但最终还是一样的。在生成一个字节[]后,我按如下方式附加它:

byte[] logo = BmpToBytes_MemStream(Properties.Resources.gcs_logo);
emailMessage.Attachments.AddFileAttachment("logo.jpg", logo);
emailMessage.Attachments[emailMessage.Attachments.Count - 1].IsInline = true;
emailMessage.Attachments[emailMessage.Attachments.Count - 1].ContentId = "gcs_logo.jpg";

private static byte[] BmpToBytes_MemStream(Bitmap bmp)
{
    byte[] bmpBytes = null;
    using (MemoryStream ms = new MemoryStream())
    {
        // Save to memory using the Jpeg format
        bmp.Save(ms, ImageFormat.Jpeg);

        // read to end
        bmpBytes = ms.GetBuffer();
        bmp.Dispose();
    }

    return bmpBytes;
}

希望这有帮助。

我认为问题在于将图像转换为base64。因此,通过将图像转换回图像,检查
imageData
中的图像是否正确

您可以使用联机工具将base64转换为映像

因此,如果是问题,请尝试如下转换图像

function toDataUrl(url, callback, outputFormat){
    var img = new Image();
    img.crossOrigin = 'Anonymous';
    img.onload = function(){
        var canvas = document.createElement('CANVAS');
        var ctx = canvas.getContext('2d');
        var dataURL;
        canvas.height = this.height;
        canvas.width = this.width;
        ctx.drawImage(this, 0, 0);
        dataURL = canvas.toDataURL(outputFormat);
        callback(dataURL);
        canvas = null; 
    };
    img.src = url;
}

toDataUrl('url', function(base64Img){
    // Base64DataURL
});

我认为问题在于将图像转换为base64。因此,通过将图像转换回图像,检查
imageData
中的图像是否正确

您可以使用联机工具将base64转换为映像

因此,如果是问题,请尝试如下转换图像

function toDataUrl(url, callback, outputFormat){
    var img = new Image();
    img.crossOrigin = 'Anonymous';
    img.onload = function(){
        var canvas = document.createElement('CANVAS');
        var ctx = canvas.getContext('2d');
        var dataURL;
        canvas.height = this.height;
        canvas.width = this.width;
        ctx.drawImage(this, 0, 0);
        dataURL = canvas.toDataURL(outputFormat);
        callback(dataURL);
        canvas = null; 
    };
    img.src = url;
}

toDataUrl('url', function(base64Img){
    // Base64DataURL
});

尝试这种方式,我已经在我的js中将图像转换为base64了@稀释剂然后您是否通过将imageData从工具转换回图像来检查imageData是否具有正确的编码图像?否我没有您使用的工具是什么@稀释用这个测试你的图像数据,试试这个方法,我已经在我的js中将图像转换成base64了@稀释剂然后您是否通过将imageData从工具转换回图像来检查imageData是否具有正确的编码图像?否我没有您使用的工具是什么@稀释测试您的图像数据与此,谢谢能回答!谢谢你能回答!