如何在Node.js中创建Blob? [英] How do I create a Blob in Node.js?

查看:3159
本文介绍了如何在Node.js中创建Blob?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试过在Node.js中创建一个Blob。首先就是这样:

I have tried to create a Blob in Node.js. First just this:

var b = new Blob(['hi', 'constructing', 'a', 'blob']);

此失败, ReferenceError:Blob未定义

然后我尝试使用 blob 模块(两行代码来自此模块的示例,请参阅 https://www.npmjs.com/package/blob ):

Then I tried with the blob module (the two lines of code is from the example for this module, see https://www.npmjs.com/package/blob):

var Blob = require('blob');
var b = new Blob(['hi', 'constructing', 'a', 'blob']);

此失败, TypeError:Blob不是构造函数

我该怎么办?

推荐答案

节点。 js没有 Blob ,它使用 缓冲区 ArrayBuffer 混淆)和类型化数组

Node.js doesn't have Blob, it uses Buffer (not to be confused with ArrayBuffer) and typed arrays.

您尝试使用的 blob npm 模块不适用于Node.js,它用于浏览器,以平滑在不同浏览器中创建 Blob 的历史差异。从其说明

The blob npm module you tried to use isn't for use in Node.js, it's for use in a browser, to smooth over historical differences in how you create Blobs in different browsers. From its description:


跨浏览器 Blob ,当回到 BlobBuilder 时适当。如果两者都不可用,则导出 undefined

A cross-browser Blob that falls back to BlobBuilder when appropriate. If neither is available, it exports undefined.

(my重点)

有些令人困惑的是,浏览器定位的软件包已经出现(成群结队) npm 过去几年。使用 require 的模块已经成为Webpack,Rollup等捆绑商的一个功能,因此人们开始使用 npm 用于浏览器的常用模块,就像将它用于Node.js的通用模块一样。事实上,一些模块被编写为在任一环境中工作。 (但 blob 似乎不是其中之一。)

Somewhat confusingly, browser-targeted packages have been appearing (in droves) on npm the last few years. Modules using require have been a feature of bundlers like Webpack, Rollup, etc., for a while, and so people started using npm for common modules for browsers just like using it for common modules for Node.js. In fact, some modules are written to work in either environment. (But blob doesn't appear to be one of them.)

在评论中,你说过你想要从Node.js进程上传文件。您不需要 Blob ,在 Node.js 中执行此操作的方式与此方法不同你在浏览器中做到这一点。所以你可能想研究如何从Node.js上传文件,而不必担心 Blob 。不过,这将是一个不同的问题。 (可以在这里回答,或者,如果您愿意使用Express, 此处。)

In comments, you've said you want to upload a file from your Node.js process. You don't need a Blob for that, the way you do this in Node.js is different from the way you do it in a browser. So you probably want to research how to upload files from Node.js, without worrying about Blobs. That would be a different question, though. (One which may be answered here or, if you're willing to use Express, here.)

这篇关于如何在Node.js中创建Blob?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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