【问题标题】:How to convert image to Base64 and include in Json as result如何将图像转换为 Base64 并作为结果包含在 Json 中
【发布时间】:2018-03-22 19:06:06
【问题描述】:

asp.net WebApi 中的 newBie。

1) 如何调整图片大小并将其转换为base64并返回给客户端。

2) 我不确定 base64 转换与图像字节数组的优缺点。

3) 在 webApi 中使用哪种格式更好 在 json 中返回 byteArray 或 Base64 中的图像。

了解这些是允许的数据类型。所以,发送图像字节是不行的。 在 JSON 中,值必须是以下数据类型之一:

    a string
    a number
    an object (JSON object)
    an array
    a boolean
    null

我需要快速处理 webApi。

非常感谢您的帮助。

谢谢

【问题讨论】:

    标签: json asp.net-web-api c#-4.0


    【解决方案1】:

    你需要使用atobbase64解码并使用btoa来编码

    您需要转义数据 在编码base64之前

        function utf8_to_b64( str ) {
          return window.btoa(unescape(encodeURIComponent( str )));
        }
    
        function b64_to_utf8( str ) {
          return decodeURIComponent(escape(window.atob( str )));
        }
    
        // Usage:
        var test1 = utf8_to_b64('✓ à la mode'); // "4pyTIMOgIGxhIG1vZGU="
        var test2 = b64_to_utf8('4pyTIMOgIGxhIG1vZGU='); // "✓ à la mode"
        
        console.log(test1);
        console.log(test2)

    为更多类型的数据提供更完整的基础https://developer.mozilla.org/fr/Add-ons/Code_snippets/StringView

    【讨论】:

    • 我没有使用 WebBrowser 或 Asp.net MVC Web 应用程序。我需要使用 C# 和网络框架在 WebApi 中处理并将结果作为 Json 返回。
    • 对不起,我想我看到 javascript stockoverflow 显示我的过滤器问题,我不知道为什么:(。
    猜你喜欢
    • 2014-11-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-24
    • 2013-11-07
    • 1970-01-01
    • 2015-12-09
    相关资源
    最近更新 更多