如何将 Base64 转换为字符数组 - C# - Microsoft Graph?

发布于 2025-01-11 16:44:12 字数 1577 浏览 0 评论 0原文

我的代码接收 Base64 值作为字符串(公共字符串 Base64Foto)。 我必须获取该值并将其转换为 Char[] 以将其发送到 Microsoft 图。

private static async Task<bool> EnviarMicrosoft(DadosApData dadosApData)
    {
        try
        {
            var scopes = new[] { "https://graph.microsoft.com/.default" };
            var tenantId = "tenantID";
            var clientId = "clientID";
            var clientSecret = "clientSecret";
            var options = new TokenCredentialOptions
            {
                AuthorityHost = AzureAuthorityHosts.AzurePublicCloud
            };


            var clientSecretCredential = new ClientSecretCredential(tenantId, clientId, clientSecret, options);

            var graphClient = new GraphServiceClient(clientSecretCredential, scopes);

            var bytes = Convert.FromBase64String(dadosApData.Base64Foto);

            var charFromByte = System.Text.Encoding.UTF8.GetString(bytes).ToCharArray();

            using var stream = new System.IO.MemoryStream(Encoding.UTF8.GetBytes(charFromByte));

            await graphClient.Users[dadosApData.Email].Photo.Content.Request().PutAsync(stream);

            return true;
        }
        catch (Exception ex)
        {
            LambdaLogger.Log(ex.Message);
            throw ex;
        }
    }

当我在 Postman 中执行此操作时,它正常工作(作为二进制文件),但是当我尝试将其传递给代码时,它不起作用,我的转换是否错误?

输入图片此处描述

PS:我收到错误:Microsoft.Fast.Profile.Core.Exception.ImageNotFoundException

My code receives a Base64 value as a String (Public String Base64Foto).
I must take this value and convert it to Char[] to send it to graph Microsoft.

private static async Task<bool> EnviarMicrosoft(DadosApData dadosApData)
    {
        try
        {
            var scopes = new[] { "https://graph.microsoft.com/.default" };
            var tenantId = "tenantID";
            var clientId = "clientID";
            var clientSecret = "clientSecret";
            var options = new TokenCredentialOptions
            {
                AuthorityHost = AzureAuthorityHosts.AzurePublicCloud
            };


            var clientSecretCredential = new ClientSecretCredential(tenantId, clientId, clientSecret, options);

            var graphClient = new GraphServiceClient(clientSecretCredential, scopes);

            var bytes = Convert.FromBase64String(dadosApData.Base64Foto);

            var charFromByte = System.Text.Encoding.UTF8.GetString(bytes).ToCharArray();

            using var stream = new System.IO.MemoryStream(Encoding.UTF8.GetBytes(charFromByte));

            await graphClient.Users[dadosApData.Email].Photo.Content.Request().PutAsync(stream);

            return true;
        }
        catch (Exception ex)
        {
            LambdaLogger.Log(ex.Message);
            throw ex;
        }
    }

When I do this in Postman, it works normally (as a binary), but when I try to pass this to the code, it doesn't work, is my conversion wrong?

enter image description here

PS: I'm Receving the error: Microsoft.Fast.Profile.Core.Exception.ImageNotFoundException

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

似梦非梦 2025-01-18 16:44:12

这是我的代码,它可以更新用户照片:

控制器:

[HttpPost]
public async Task UploadAsync(IFormFile file) {
    var ms = new MemoryStream();
    file.CopyTo(ms);
    var fileBytes = ms.ToArray();
    //string s = Convert.ToBase64String(fileBytes);

    var scopes = new[] { "https://graph.microsoft.com/.default" };
    var tenantId = "xx.onmicrosoft.com";
    var clientId = "client_id";
    var clientSecret = "client_secret";
    var options = new TokenCredentialOptions
    {
        AuthorityHost = AzureAuthorityHosts.AzurePublicCloud
    };
    var clientSecretCredential = new ClientSecretCredential(
        tenantId, clientId, clientSecret, options);
    var graphClient = new GraphServiceClient(clientSecretCredential, scopes);
    var res = await graphClient.Users["user_id"].Photo.Content.Request().PutAsync(new MemoryStream(fileBytes, 0, fileBytes.Length));
}

cshtml:

<input id="picContent" type="file" name="photo" value="please upload a picture" />
<input id="btn1" type="button" value="upload"/>

@section scripts
{
    <script>
        $("#btn1").click(function(){
            var formData = new FormData();
            var img = $("#picContent")[0].files[0];
            console.log(img);
            console.log($("#picContent").val());
            formData.append("file", img);
            $.ajax({
                data: formData,
                url:"https://localhost:44340/home/upload",
                type:"post",
                data:formData,
                processData: false,
                contentType: false,
                success:function(data){
                    alert(data);
                }
            })        
        })

    </script>
}

This is my code and it can update the user photo:

Controller:

[HttpPost]
public async Task UploadAsync(IFormFile file) {
    var ms = new MemoryStream();
    file.CopyTo(ms);
    var fileBytes = ms.ToArray();
    //string s = Convert.ToBase64String(fileBytes);

    var scopes = new[] { "https://graph.microsoft.com/.default" };
    var tenantId = "xx.onmicrosoft.com";
    var clientId = "client_id";
    var clientSecret = "client_secret";
    var options = new TokenCredentialOptions
    {
        AuthorityHost = AzureAuthorityHosts.AzurePublicCloud
    };
    var clientSecretCredential = new ClientSecretCredential(
        tenantId, clientId, clientSecret, options);
    var graphClient = new GraphServiceClient(clientSecretCredential, scopes);
    var res = await graphClient.Users["user_id"].Photo.Content.Request().PutAsync(new MemoryStream(fileBytes, 0, fileBytes.Length));
}

cshtml:

<input id="picContent" type="file" name="photo" value="please upload a picture" />
<input id="btn1" type="button" value="upload"/>

@section scripts
{
    <script>
        $("#btn1").click(function(){
            var formData = new FormData();
            var img = $("#picContent")[0].files[0];
            console.log(img);
            console.log($("#picContent").val());
            formData.append("file", img);
            $.ajax({
                data: formData,
                url:"https://localhost:44340/home/upload",
                type:"post",
                data:formData,
                processData: false,
                contentType: false,
                success:function(data){
                    alert(data);
                }
            })        
        })

    </script>
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文