【问题标题】:Nativescript uploading an image on a nodejs server that uses formidable to handle inputsNativescript 在使用强大处理输入的 nodejs 服务器上上传图像
【发布时间】:2016-07-05 16:42:32
【问题描述】:

您好,我想知道是否有人可以帮助我通过使用强大的 js 处理输入的 nodejs 服务器从 nativescript 上传相机模块拍摄的图像

下面是我的 nativescript 代码:

下面是nodejs服务器代码:

尝试将图像转换为Base64String(“JPG”),仍然不起作用(代码如下:)

nodejs 服务器输出

但我的 nativescript 代码似乎不起作用

【问题讨论】:

  • 考虑使用代码 sn-ps 而不是屏幕截图来简化代码复制。 base64 和 utf8 来自哪里(不是 NativeScript APi 的一部分)此外,您正在从图像文件中读取文本(不确定这是否按预期工作)。您可以将图像源与 toBase64String 一起使用来生成 base64 docs.nativescript.org/api-reference/classes/…
  • @NickIliev 尝试将我的图像转换为 base64 它仍然无法正常工作,服务器仍然没有收到任何内容:(
  • 尝试在已知的虚拟服务器(如 httpbin 或其他已知服务)上进行测试。也尝试记录服务器错误或响应
  • @NickIliev 制作了我自己的服务器,我的 nativescript 代码似乎可以工作,尝试在上面的 nodejs 服务器上上传图像也似乎可以工作。但是当我尝试将来自 nativescript 应用程序的图像上传到 nodejs 服务器时,仍然无法正常工作:(
  • 你是在 iOS 上部署的吗!?如果是 - 请记住,不允许使用 http 传输协议 - 您应该使用 https 或“破解” info.plist(仅用于测试目的)stackoverflow.com/questions/30731785/…

标签: javascript node.js camera nativescript formidable


【解决方案1】:

这是一种非常简单的方法,可以将图像源从 cameraModule 转换为 base64String,然后再将其传递给您的服务器

 cameraModule.takePicture().then(function(imageSource) {
    var imageAsBase64String = imageSource.toBase64String("JPG");
    return imageAsBase64String; 
}).then(function (imageAsBase64String) {
    http.request({
        url: "https://httpbin.org/post",
        method: "POST",
        headers: { "Content-Type": "application/json" },
        content: JSON.stringify({ name: "myName", imageAsString: imageAsBase64String })  
    }).then(function (response) {
        var result = response.content.toJSON();

        console.log("args: " + result.args);
        console.log("origin: " + result.origin);
        console.log("headers: " + result.headers);
        console.log("json: " + result.json);
        console.log("url: " + result.url);
        // console.log("data: " + result.data); // this is our send content

        var myObj = JSON.parse(result.data); // as we are passing a stringied JSON we ahve to parse it
        console.log("my Image Name: " + myObj.name);
        console.log("my Image Base64String: " + myObj.imageAsString);

    }, function (e) {
        console.log("Error occurred " + e);
    });
})

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-05-08
    • 2021-05-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-21
    • 2018-11-04
    相关资源
    最近更新 更多