Nativescript 图像到 Base64 然后获取 POST [英] Nativescript image to Base64 then fetch POST

查看:20
本文介绍了Nativescript 图像到 Base64 然后获取 POST的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How can I Base64 an image so it would be suitable to send with nativescript fetch module?

推荐答案

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

Here is a basic demo for your case using test server

"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;

这篇关于Nativescript 图像到 Base64 然后获取 POST的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆