node.js使用url下载文件
来源:互联网 发布:java三维地质建模 编辑:程序博客网 时间:2024/06/16 21:56
采用http协议来实现文件下载。其优点在于不需要以来额外程序来下载文件
使用curl来下载文件,我们需要采用child_process模块中的spawn方法。
另外的方法是使用wget,这种方法的代码非常简洁。
关于child_process.spawn
与
child_process.exec最大的不同之处在于返回值:
spawn returns a stream and exec returns a buffer.
child_process.spawn
returns an object with stdout
and stderr
streams. You can tap on thestdout
stream to read data that the child process sends back to Node. stdout
being a stream has the "data", "end", and other events that streams have. spawn
is best used to when you want the child process to return a large amount of data to Node - image processing, reading binary data etc.
child_process.spawn
is "asynchronously asynchronous", meaning it starts sending back data from the child process in a stream as soon as the child process starts executing.
child_process.exec
returns the whole buffer output from the child process. By default the buffer size is set at 200k. If the child process returns anything more than that, you program will crash with the error message "Error: maxBuffer exceeded". You can fix that problem by setting a bigger buffer size in the exec options. But you should not do it because exec
is not meant for processes that return HUGE buffers to Node. You should use spawn
for that. So what do you use exec
for? Use it to run programs that return result statuses, instead of data.
child_process.exec
is "synchronously asynchronous", meaning although the exec is asynchronous, it waits for the child process to end and tries to return all the buffered data at once. If the buffer size of exec
is not set big enough, it fails with a "maxBuffer exceeded" error.
另外,node作为server可以实现upload以及download功能。
如下的code是download的功能。
Code的下载功能在于header中的attachement选项。
如下的code是upload功能。 Upload的文件在req.files中体现。
对于上传的文件,一种方法是保存在server的指定目录下,另外一种方法是保存在MongoDB中。
- node.js使用url下载文件
- Node.js:使用request模块下载文件
- node.js下载文件
- js通过Url下载文件
- jQuery 插件取url参数[jquery.url.js]的使用以及文件下载
- 基于Node.js的文件下载服务器
- 基于Node.js的文件下载服务器
- Node.js简介url
- node.js URL解析
- node.js url
- node.js:url
- Node.js URL模块
- node.js之Url
- Node.js url.parse()
- node.js url模块
- Node.js url模块
- node.js url 模块使用时的小trick
- 使用URL Moniker 异步下载文件
- 使用NvEncoder编码为H264数据时的输出
- PAT (Advanced Level) Practise 1075 PAT Judge (25)
- CALayer的mask属性简介
- 【二分查找】查找数组中第一个比k大的数的下标
- 「LibreOJ NOI Round #1」接竹竿 (dp+前缀和优化)
- node.js使用url下载文件
- Tensorflow结构简介
- java设计模式-观察者模式详解
- iOS CAReplicatorLayer复制图层 ---- 加载指示器制作
- Volley框架之三 疑难实现
- ASCII码表
- 访问nginx默认页报403错误解决方法
- velocity模板引擎学习
- angular页面传参的问题