Nativescript image to Base64 then fetch POST

社会主义新天地 提交于 2019-12-24 00:57:26

问题


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


回答1:


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;


来源:https://stackoverflow.com/questions/38368879/nativescript-image-to-base64-then-fetch-post

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!