【发布时间】:2015-09-24 23:31:36
【问题描述】:
我在服务器端使用 NodeJs,但我需要在文本文件中重写(擦除它的内容并重新写入)或在不使用 ftp、Web 服务器或其他东西的情况下将其发送到同一网络中的 RaspberryPi在覆盆子里。
我一直在阅读有关 NodeJs 的 'request' 的信息,但我认为我需要在 Raspberry 中安装类似 Web 服务器的东西才能对 Raspberry 中的某个 URL 执行 'post'。
function uploadFile() {
var formData = {
// Pass a simple key-value pair
my_field: 'my_value',
// Pass data via Buffers
my_buffer: new Buffer([1, 2, 3]),
// Pass data via Streams
my_file: fs.createReadStream(__dirname + '/unicycle.jpg'),
// Pass multiple values /w an Array
attachments: [
fs.createReadStream(__dirname + 'porton/json.txt')
],
// Pass optional meta-data with an 'options' object with style: {value: DATA, options: OPTIONS}
// Use case: for some types of streams, you'll need to provide "file"-related information manually.
// See the `form-data` README for more information about options: https://github.com/felixge/node-form-data
custom_file: {
value: fs.createReadStream('/dev/urandom'),
options: {
filename: 'topsecret.jpg',
contentType: 'image/jpg'
}
}
};
request.post({ url: '<RaspberryStaticIP>/route/message.txt', formData: formData }, function optionalCallback(err, httpResponse, body) {
if (err) {
return console.error('upload failed:', err);
}
console.log('Upload successful! Server responded with:', body);
});
}
https://github.com/request/request#multipartform-data-multipart-form-uploads
使用curl 进行多部分上传
curl('127.0.0.1/upload.php', {
MULTIPART: [
{name: 'file', file: '/file/path', type: 'text/html'},
{name: 'sumbit', contents: 'send'}
]
}, function(e) {
console.log(e);
console.log(this.body);
this.close()
});
https://www.npmjs.com/package/node-curl#multipart-upload
或者,是否可以使用'fs' 但无需本地化?
fs.writeFile('<RaspberryStaticIP>/route/message.txt', '123', function (err) {
if (err) throw err;
console.log('It\'s saved!');
});
是否有另一种方法可以仅使用 NodeJs(及其任何实现)或 PHP 来做到这一点?如果无法按照我说的方式进行操作,那么最好的方法是通过 FTP、Raspberry 中的 WebServer 或任何推荐方法进行操作。
【问题讨论】:
标签: php node.js curl ftp raspberry-pi