Javascript Node.js 不能创建 Blob?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14653349/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Node.js can′t create Blobs?
提问by zer02
I am working with node.js and I streamed my Audio to my node.js server. Now I noticed during the process of building the audio blob:
我正在使用 node.js 并将我的音频流式传输到我的 node.js 服务器。现在我在构建音频 blob 的过程中注意到:
var audioBlob = new Blob([dataview], { type: 'audio/wav' });
That I get a ReferenceError at new Blob. It seems that Blob is not supported. How can I create a blob which I would like to save with node.js fs module.
我在新的 Blob 上得到了一个 ReferenceError。似乎不支持 Blob。如何创建一个我想用 node.js fs 模块保存的 blob。
Thanks guys!
谢谢你们!
采纳答案by zer02
The Solution to this problem is to create a function which can convert between Array Buffers and Node Buffers. :)
这个问题的解决方案是创建一个可以在数组缓冲区和节点缓冲区之间转换的函数。:)
Convert a binary NodeJS Buffer to JavaScript ArrayBuffer
将二进制 NodeJS 缓冲区转换为 JavaScript ArrayBuffer
In recent node versions it's just:
在最近的节点版本中,它只是:
let buffer = Buffer.from(arraybuffer);
let arraybuffer = Uint8Array.from(buffer).buffer;
回答by Richie Bendall
Just use cross-blob:
只需使用cross-blob:
const Blob = require("cross-blob");
new Blob([]);
//=> Blob {size: 0, type: ""}
// Global patch (to support external modules like is-blob).
globalThis.Blob = Blob;
回答by Rooster
Another solution to consider is to use a Base64 Stringto transfer data from the Server to the client.
另一个需要考虑的解决方案是使用Base64 字符串将数据从服务器传输到客户端。
I am working on a Node.js project where I receive audio data in the form of an ArrayBuffer, and I want to send and play that data in the browser. Most of my struggles came from trying to send the ArrayBuffer to the client or trying to convert the ArrayBuffer and send a Buffer.
我正在处理一个 Node.js 项目,我以 ArrayBuffer 的形式接收音频数据,我想在浏览器中发送和播放该数据。我的大部分挣扎来自尝试将 ArrayBuffer 发送到客户端或尝试转换 ArrayBuffer 并发送一个缓冲区。
What ended up being a simple solution for me was to
最终对我来说是一个简单的解决方案是
- Convert the ArrayBuffer to a Base64 encoded String on the Server
- Return/Send the Base64 String to the client from the server
- Create an Audio element/object on the client side and play the sound
- 在服务器上将 ArrayBuffer 转换为 Base64 编码的字符串
- 从服务器返回/发送 Base64 字符串到客户端
- 在客户端创建一个 Audio 元素/对象并播放声音
I used base64-arraybufferto perform the ArrayBuffer > Base64 String conversion (though, it may be simple to do this without a package).
我使用base64-arraybuffer来执行 ArrayBuffer > Base64 String 转换(不过,在没有包的情况下执行此操作可能很简单)。
I used tips from hereto create the audio element on the client side.
我使用这里的技巧在客户端创建音频元素。
*I haven't done much in the way of testing limits - so I don't know how this might handle large audio files.
*我在测试限制方面做得不多 - 所以我不知道这可能如何处理大型音频文件。
回答by sent1nel
As a suggestion, you might want to read this: http://howtonode.org/really-simple-file-uploads
作为建议,您可能需要阅读以下内容:http: //howtonode.org/really-simple-file-uploads
I mean I guess I don't know what you're trying to do. There may not be a module for blobs, but if you want to just write something to disk, there's the fs module.. This code won't work directly, but..
我的意思是我想我不知道你想做什么。可能没有用于 blob 的模块,但是如果您只想将某些内容写入磁盘,则可以使用 fs 模块。此代码不能直接工作,但是...
var fs = require('fs')
, express = require('express')
app.post('/upload', function (req, res) {
// asynch call to write file to disk
fs.write("/tmp/file.mp3", req.params.body, function (err) {
if (err) console.log(err)
});
res.end();
});
Simply post an mp3, or anything really, to /upload, and it'll write it to disk. You can do whatever validation you want.
只需将 mp3 或任何真正的内容发布到 /upload,它就会将其写入磁盘。你可以做任何你想做的验证。

