【问题标题】:Sending image Buffer through API to add to IPFS通过 API 发送图像缓冲区以添加到 IPFS
【发布时间】:2018-08-30 11:27:50
【问题描述】:

我正在尝试将图像上传到 IPFS。我正在这样做:

我从我的 Web UI 上传图像,将其转换为我的 Angular 组件中的缓冲区,通过 put/post 请求(使用 httpclient)将其发送到我的 nodeJS Express 服务器,我的服务器连接到 IPFS 网络并保存图像.

我遇到的问题是我无法将图像上传到 IPFS,并且我不断收到此错误:

Content submission error: Error: Invalid arguments, data must be an object, Buffer or readable stream 

这就是我在发送之前获取缓冲区的方式:

const that = this;
      that.ConfirmButtonDisabled=true;
        let NativeInput = jQuery(this.ImageInput.nativeElement);
        NativeInput.trigger('click');
        NativeInput.change('file-select',function (file) {
            const reader = new FileReader();
            reader.onload = (e) => {
                let file=reader.result
                let buffer = new Buffer(file);
                const buff: ArrayBuffer = buffer.buffer;
                that.CustomerInfo["KImage"] = buff;
                console.log("read the file into buffer", that.CustomerInfo)
                that.ConfirmButtonDisabled=false;
            }
            reader.readAsArrayBuffer(NativeInput.prop('files')[0]);
            console.log(NativeInput.prop('files')[0])
            that.SelectedImage = NativeInput.prop('files')[0].name;
        })

这样登录浏览器时,我会得到如下信息:image:ArrayBuffer(6924) {}

如果我不使用 buffer.buffer,我会得到一个这样记录的 uint8array: image:Uint8Array(6924) [255, 216, 255, 224, 0, 16, 74, 70, 73, 70, ... ]

如果我在服务器端登录,最后一个版本的显示方式不同, 数组的每个元素都会更改为 \u0000 之类的东西,就好像它们是 ascii 或 utf8 字符(这可能是 console.log() 函数的工作,但我不确定) 例如:����\u0000\u0010JFIF\u0000\u0001\u0001\u0000\u0000\u0001\u0000\u0001\u0000\u0000��\u0000�\u0000\t\u0006\u0007\r\r\r\u0010\r\r\u0010\u000f\r\r\r\u000e\r\r\r\r

在这两种情况下,IPFS 都会输出相同的错误。

注意:我确实使用 Web 客户端直接使用 IPFS(没有 nodeJS 服务器),并且它有效。所以我认为相同的代码会起作用,因为它都是 javascript。

我该怎么做?我如何通过nodeJS正确地将缓冲区(或图像)从我的angular 5客户端发送到IPFS;

【问题讨论】:

    标签: javascript jquery node.js angular ipfs


    【解决方案1】:

    我通过 HTTP 发送 uint8array 并在我的 nodeJs 代码中使用 buffer.from 创建一个缓冲区来解决此问题。一个相当简单的修复。

    【讨论】:

    • 如何将 uint8array 转换回图像文件?来自nodejs
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-20
    • 1970-01-01
    • 2019-12-22
    • 2010-11-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多