如何使用 node.js 发送文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9321027/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 15:12:17  来源:igfitidea点击:

How to send files with node.js

node.jsexpress

提问by ajsie

How do you send files on node.js/express.

你如何在 node.js/express 上发送文件。

I am using Rackspace Cloudfiles and wanna send images/videos to their remote storage but I am not sure that it's as simple as reading the file (fs.readFileSync()) and send the data in the request body, or is it?

我正在使用 Rackspace Cloudfiles 并想将图像/视频发送到他们的远程存储,但我不确定它是否像读取文件 (fs.readFileSync()) 并在请求正文中发送数据一样简单,或者是吗?

What should the headers be.

标题应该是什么。

What if it's a very large file on a couple of GBs?

如果它是几个 GB 上的非常大的文件怎么办?

Is it possible to use superagent (http://visionmedia.github.com/superagent) for this or is there a better library for sending files?

是否可以为此使用超级代理(http://visionmedia.github.com/superagent),或者是否有更好的库来发送文件?

Please give me some information about this.

请给我一些关于这方面的信息。

Thanks!

谢谢!

回答by denysonique

app.get('/img/bg.png', function(req, res) {
  res.sendFile('public/img/background.png')
})

https://expressjs.com/en/api.html#res.sendFile

https://expressjs.com/en/api.html#res.sendFile

use "res.sendFile". "res.sendfile" is deprecated.

使用“res.sendFile”。“res.sendfile”已弃用。

回答by Peter Lyons

For large files, you will want to use node.js's concept of piping IO streams together. You want to open the local file for reading, start the HTTP request to rackspace, and then pipe the data events from the file read process to the HTTP request process.

对于大文件,您将需要使用 node.js 将 IO 流管道连接在一起的概念。您要打开本地文件进行读取,向rackspace 启动HTTP 请求,然后将文件读取进程中的数据事件通过管道传输到HTTP 请求进程。

Here's an article on how to do this.

这是一篇关于如何做到这一点的文章

Superagent is fine for small files, but because the superagent API presumes your entire request body is loaded into memory before starting the request, it's not the best approach for large file transfers.

Superagent 适用于小文件,但由于 superagent API 假定您的整个请求正文在开始请求之前已加载到内存中,因此它不是大文件传输的最佳方法。

Normally you won't need to worry specifically about the request headers as node's HTTP request library will send the appropriate headers for you. Just make sure you use whatever HTTP method your API requires (probably POST), and it looks like for rackspace you will need to add the X-Auth-Tokenextra header with your API token as well.

通常你不需要特别担心请求头,因为节点的 HTTP 请求库会为你发送适当的头。只要确保您使用 API 需要的任何 HTTP 方法(可能是 POST),并且对于机架空间,您似乎还需要X-Auth-Token使用 API 令牌添加额外的标头。

回答by Linus Gustav Larsson Thiel

I am using Rackspace Cloudfiles and wanna send images/videos to their remote storage but I am not sure that it's as simple as reading the file (fs.readFileSync()) and send the data in the request body, or is it?

我正在使用 Rackspace Cloudfiles 并想将图像/视频发送到他们的远程存储,但我不确定它是否像读取文件 (fs.readFileSync()) 并在请求正文中发送数据一样简单,或者是吗?

You should neveruse fs.readFileSyncin general. When you use it, or any other method called somethingSync, you block the entire serverfor the duration of that call. The onlyacceptable time to make synchronous calls in a node.js program is during startup.

你应该永远不会使用fs.readFileSync一般。当您使用它或任何其他称为 的方法时somethingSync,您会在该调用期间阻塞整个服务器。在 node.js 程序中进行同步调用的唯一可接受的时间是在启动期间。

What should the headers be.

标题应该是什么。

See RackSpace Cloud Files API.

请参阅RackSpace 云文件 API

Is it possible to use superagent (http://visionmedia.github.com/superagent) for this or is there a better library for sending files?

是否可以为此使用超级代理(http://visionmedia.github.com/superagent),或者是否有更好的库来发送文件?

While I don't have any experience with superagent, I'm sure it will work fine. Just make sure you read the API documentation and make your requests according to their specification.

虽然我没有任何使用超级代理的经验,但我相信它会正常工作。只需确保您阅读 API 文档并根据其规范提出请求即可。