mirror of
https://github.com/node-red/node-red.git
synced 2023-10-10 13:36:53 +02:00
File node: allow filename to be overridden
This commit is contained in:
parent
ae03562f86
commit
cef652eef7
@ -36,7 +36,9 @@
|
|||||||
</script>
|
</script>
|
||||||
|
|
||||||
<script type="text/x-red" data-help-name="file">
|
<script type="text/x-red" data-help-name="file">
|
||||||
<p>Writes the <b>msg.payload</b> to the file specified, e.g. to create a log.</p>
|
<p>Writes <b>msg.payload</b> to the file specified, e.g. to create a log.</p>
|
||||||
|
<p>The filename can be overridden by the <code>filename</code> property
|
||||||
|
of the incoming message.</p>
|
||||||
<p>A newline is added to every message. But this can be turned off if required, for example, to allow binary files to be written.</p>
|
<p>A newline is added to every message. But this can be turned off if required, for example, to allow binary files to be written.</p>
|
||||||
<p>The default behaviour is to append to the file. This can be changed to overwrite the file each time, for example if you want to output a "static" web page or report.</p>
|
<p>The default behaviour is to append to the file. This can be changed to overwrite the file each time, for example if you want to output a "static" web page or report.</p>
|
||||||
</script>
|
</script>
|
||||||
@ -46,7 +48,7 @@
|
|||||||
category: 'storage-output',
|
category: 'storage-output',
|
||||||
defaults: {
|
defaults: {
|
||||||
name: {value:""},
|
name: {value:""},
|
||||||
filename: {value:"",required:true},
|
filename: {value:""},
|
||||||
appendNewline: {value:true},
|
appendNewline: {value:true},
|
||||||
overwriteFile: {value:false}
|
overwriteFile: {value:false}
|
||||||
},
|
},
|
||||||
|
@ -25,22 +25,28 @@ function FileNode(n) {
|
|||||||
this.overwriteFile = n.overwriteFile;
|
this.overwriteFile = n.overwriteFile;
|
||||||
var node = this;
|
var node = this;
|
||||||
this.on("input",function(msg) {
|
this.on("input",function(msg) {
|
||||||
var data = msg.payload;
|
var filename = msg.filename || this.filename;
|
||||||
if (this.appendNewline) {
|
|
||||||
data += "\n";
|
if (filename == "") {
|
||||||
}
|
node.warn('No filename specified');
|
||||||
if (this.overwriteFile) {
|
} else {
|
||||||
fs.writeFile(this.filename, data, function (err) {
|
var data = msg.payload;
|
||||||
if (err) node.warn('Failed to write to file : '+err);
|
if (this.appendNewline) {
|
||||||
//console.log('Message written to file',this.filename);
|
data += "\n";
|
||||||
});
|
}
|
||||||
}
|
if (this.overwriteFile) {
|
||||||
else {
|
fs.writeFile(filename, data, function (err) {
|
||||||
fs.appendFile(this.filename, data, function (err) {
|
if (err) node.warn('Failed to write to file : '+err);
|
||||||
if (err) node.warn('Failed to append to file : '+err);
|
//console.log('Message written to file',this.filename);
|
||||||
//console.log('Message appended to file',this.filename);
|
});
|
||||||
});
|
}
|
||||||
}
|
else {
|
||||||
|
fs.appendFile(filename, data, function (err) {
|
||||||
|
if (err) node.warn('Failed to append to file : '+err);
|
||||||
|
//console.log('Message appended to file',this.filename);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user