【问题标题】:Nativescript image to Base64 then fetch POSTNativescript 图像到 Base64 然后获取 POST
【发布时间】:2016-07-14 08:13:50
【问题描述】:

如何对图像进行 Base64 处理,使其适合使用 nativescript fetch 模块发送?

【问题讨论】:

    标签: post base64 fetch nativescript


    【解决方案1】:

    这是使用测试服务器的案例的基本演示

    "use strict";
    var imageSourceModule = require("image-source");
    
    function navigatingTo(args) {
    
        var page = args.object;
    
        // using icon.png from the template app from res://icon
        var imgSrc = imageSourceModule.fromResource("icon");
        var imageAsBase64 = imgSrc.toBase64String("PNG");
    
        fetch("https://httpbin.org/post", {
            method: "POST",
            headers: { "Content-Type": "application/octet-stream" },
            body: imageAsBase64
        }).then(function (r) { return r.json(); }).then(function (result) {
            console.log("Base64String: " + result.data);
    
            // for (var key in result) {
            //     if (result.hasOwnProperty(key)) {
            //         var element = object[result];
            //         console.log(key);
            //         console.log(element);
            //         console.log("------")
            //     }
            // }
    
        }, function (e) {
            console.log("Error occurred " + e);
        });
    }
    exports.navigatingTo = navigatingTo;
    

    【讨论】:

    • 正是我想要的!
    • 你应该得到一个大胖子......竖起大拇指!
    • @Nick lliev,在 nativescript 中将位图转换为 Base64 有什么想法吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多