File Upload in KoaJS - node.js

I'm trying to upload files using the code from example. But it's not working.
In the controller I wrote the code
var parse = require('co-busboy');
var fs = require('fs');
var path = require('path');
var parts = parse(this);
var part;
while (part = yield parts) {
var stream = fs.createWriteStream('/tmp/' + part.filename);
part.pipe(stream);
console.log('uploading %s -> %s', part.filename, stream.path);
}
But when I upload images in the console I only get new uploading location. But on new location images are not saving.
Any Solution?
Thanks in Advance,
Nixon

I got the answer. I just had to remove '/' before the 'tmp'.
So the code is like this
while (part = yield parts) {
var stream = fs.createWriteStream('tmp/' + part.filename);
part.pipe(stream);
console.log('uploading %s -> %s', part.filename, stream.path);
}

Related

How can I get the path of an excel file with nodeJS in a chatbot using Bot framework?

I have done a chatbot with bot framework and with this framework it is possible to add an attachement.
So I have done a code to have my excel file in base64 after I add it in my chatbot.
But I want to take an Excel file from everywhere in my pc and to transform it in base64 I need to have the full path and in NodeJS I don't know how to do it.
async attachmentsStep(stepContext, next) {
var fs = require('fs');
var activity = stepContext.context.activity;
if (activity.attachments && activity.attachments.length > 0) {
var attachment = activity.attachments[0];
// function to encode file data to base64 encoded string
function base64_encode(file) {
// read binary data
var bitmap = fs.readFileSync(file);
// convert binary data to base64 encoded string
return new Buffer.from(bitmap).toString('base64');
}
this.base64str = base64_encode( **PATH OF EXCEL FILE** + attachment.name);
var nex = await stepContext.next();
var base64 = this.base64str;
return {
base64,
nex
};
}
}
Do you have an idea please ?
You can use the __filename and __dirname for getting the file's absolute path.
console.log(__filename);
// Prints: /Users/mjr/example.js
ContentUrl recovers the file so not need the path and with the url I have directly convert it in base64 like this :
async attachmentsStep(stepContext, next) {
var activity = stepContext.context.activity;
if (activity.attachments && activity.attachments.length > 0) {
var attachment = activity.attachments[0];
var base64Url = attachment.contentUrl;
console.log(process.env.PATH);
/** Convert Url in base64 **/
var axios = require('axios');
var excel = await axios.get(base64Url, {responseType: 'arraybuffer'});
var base64str = Buffer.from(excel.data).toString('base64');
/**************************/
// base64str = 'data:' + base64Type + ';base64,' + base64str;
var nex = await stepContext.next();
return {
base64str,
nex
};
}
}

Node js App to display folder files

I am trying to display file list from folders .
my folder structure is like below
Invoices
1. error
2. processed
3. unprocessed
I have created node api for same which i am calling on my html page. code for the same is as below
const fs = require('fs');
var express = require('express');
var cors = require('cors');
var app = express();
app.use(cors());
app.use(express.static(__dirname));
var flength;
var filename;
var currentFile;
var items = [];
var dir1 = 'Invoices';
var filepath = [];
var readFolder = function(dir1) {
var countt = function(filename) {
var currentFile = dir1 + '/' + filename;
fs.readdir(currentFile, (err, files) => {
flength = files.length;
var fileArrayList = [];
for (var f in files) {
var record = {
filename: files[f],
filepath: dir1 + '/' + filename + '/' + files[f]
}
fileArrayList.push(record);
}
items.push({
'file': filename,
'count': flength,
'files': fileArrayList
});
});
}
var ReadFirst = function(dir1) {
fs.readdir(dir1, (err, files) => {
for (var i in files) {
var filename = files[i];
var currentFile = dir1 + '/' + filename;
var stats = fs.statSync(currentFile);
if (stats.isDirectory()) {
countt(filename);
}
}
});
}
ReadFirst(dir1);
}
setTimeout(function(str1, str2) {
readFolder(dir1);
}, 1000);
app.get('/FileCount', function(req, res) {
res.send(items);
});
app.listen(4000);
console.log('Listening on port 4000');
When i add or delete files from any folder then its not reflecting on my html page.need help for this.
thank you.
This is happening because of the way you've implemented this.
Your client (the HTML page) requests the server (NodeJS) API for
some data. In this case, it is the list of files in a folder. The server sends the response based on the state of files at the time (plus ∆).
You display those results in the HTML page. Now, there is no live link between your HTML page and your backend server. This means any changes that happen after this point won't be automatically reflected in the page.
You can do two things here:
Call your API repeatedly after an interval of few seconds. (If you're using AngularJS, look into setTimeout function.
Use sockets for having a real-time link. One good documentation here:
https://loopback.io/doc/en/lb2/Realtime-socket-io.html
This is more of a design issue and your NodeJS API looks fine.

Output looped PIWIK API call to CSV

I've got the following problem: I am looping through API calls using different dates to append the output to a CSV file. However, the output data in the CSV file only contains data from the first date.
When I log the results to the command prompt I do get multiple dates, meaning the problem occurs when writing the output to the CSV.
Moment.js is used for setting the start and end date to loop through and fast-csv to write the output of the API call to a CSV file.
// load and configure
const piwik = require ('piwik').setup ('placeholderurl', 'XXXXX');
// filesystem requirement
var fs = require('fs');
// fast-csv requirement
var csv = require("fast-csv");
// moment.js requirement
var moment = require('moment');
// variabelen voor het loopen door datums
var a = moment().format('2016-05-12');
var b = moment().format('2016-05-15');
var stream = fs.createWriteStream ('my.csv', {flags: 'a'})
// samenstellen API url
for (var m = moment(a); m.isBefore(b); m.add(1, 'days')) {
piwik.api (
{
method: 'Live.getLastVisitsDetails',
idSite: 3,
period: 'day',
format: 'csv',
date: moment(m).format('YYYY-MM-DD')
},
function (err, data) {
if (err) {
console.log (err);
return;
}
console.log(data)
csv
.writeToStream(fs.createWriteStream("my.csv"), data, {flags: 'a', headers: true});
}
);
}
API token and url removed for privacy reasons.
Solved. Got rid of the PIWIK API package and decided to use HTTP GET to retrieve the url manually.
The code:
// http requirement
var http = require('http');
var request = require('request');
// filesystem requirement
var fs = require('fs');
// moment.js requirement
var moment = require('moment');
// variabelen voor het loopen door datums
var a = moment().format('2016-05-12');
var b = moment().format('2016-05-15');
var m = moment(a);
//var stream = fs.createWriteStream ('my.csv', {flags: 'a'})
// samenstellen API url
for (var m = moment(a); m.isBefore(b); m.add(1, 'days')) {
request
.get("http://placeholder.com/?module=API&method=Live.getLastVisitsDetails&idSite=3&period=day&date=" + moment(m).format('YYYY-MM-DD') + "&format=csv&token_auth=placeholdertoken&filter_limit=-1")
.on('error', function(err) {
console.log(err)
})
.pipe(fs.createWriteStream('data-' + moment(m).format('YYYY-MM-DD') + '.csv'))
console.log(moment(m).format('YYYY-MM-DD') + " " + "saved")
}

image upload with "post" in nodejs with request module

How do we upload an image received form the mobile app to another server using the request module in nodejs?
I have tried using the multipart module to extract the file and send it in formData attribute with the request module (post method). This doesn't seem to work.
Please Use following code, it has been tested in express. you can modify it according to your requirement
var path = require('path');
var util = require('util');
if (req.files.profile_image !== undefined) {
var file = req.files.profile_image;
var tmp_path = file.path;
var fileName = file.name;
var milliseconds = new Date().getTime();
var file_ext = fileName.substr((Math.max(0, fileName.lastIndexOf(".")) || Infinity) + 1);
var newFileName = requestedUser + '_' + milliseconds + '.' + file_ext;
var pathToFile = require('path').dirname(require.main.filename);
var mainPath = path.dirname(pathToFile)
var target_path = path.join(mainPath, 'public/uploads/users', newFileName);
var readStream = fs.createReadStream(tmp_path)
var writeStream = fs.createWriteStream(target_path);
util.pump(readStream, writeStream, function(err) {
if (err) {
//handle error
} else {
//successfully uploaded
}
});
} else {
//file not recieved
}
Thanks

Node appendFile not appending data chunk to file on filesystem

I have a program that is trying to get the values from the request using curl and store them in a file and serve the stored content back. The decision to store or append the contents in file are based on a query parameter appendFlag
Now when i run this program what i am getting in console is "true" and "appending" This suggests that it indeed reads the flag goes to the if part but somehow the appendFile function is not working.
var http = require('http');
var url = require('url');
var querystring = require('querystring');
var fs = require('fs');
http.createServer(function(request,response){
var str = request.url.split('?')[1];
var query = querystring.parse(str);
var writeStream = fs.createWriteStream(query['fileName']);
console.log("query - ");
console.log(query["appendFlag"]);
request.on('data',function(chunk){
if(query["appendFlag"]=="true"){
console.log("appending");
fs.appendFile(query['fileName'],chunk.toString(),function(err){
if(err) throw err;
});
}else{
var bufferGood = writeStream.write(chunk);
if(!bufferGood) request.pause();
}
});
request.on('end',function(){
response.writeHead(200);
response.write("\n Content with this url is - \n");
var readStream = fs.createReadStream(query['fileName'],{bufferSize:64*1024});
readStream.on('data',function(chunk){
response.write(chunk.toString());
});
readStream.on('end',function(){
response.write("\n");
response.end();
});
});
writeStream.on('drain',function(){
request.resume();
});
}).listen(8080);
Then after reading an answer from SO( How to create appending writeStream in Node.js ) i tried -
// Program to extract url from the request and writing in that particular file
var http = require('http');
var url = require('url');
var querystring = require('querystring');
var fs = require('fs');
http.createServer(function(request,response){
var str = request.url.split('?')[1];
var query = querystring.parse(str);
var writeStream = fs.createWriteStream(query['fileName']);
options = {
'flags': 'a' ,
'encoding' : null,
'mode' : 0666
}
var appendStream = fs.createWriteStream(query['fileName'],[options]);
console.log("query - ");
console.log(query["appendFlag"]);
request.on('data',function(chunk){
if(query["appendFlag"]=="true"){
console.log("appending");
var bufferGood = appendStream.write(chunk.toString());
if(!bufferGood) request.pause();
}else{
var bufferGood = writeStream.write(chunk.toString());
if(!bufferGood) request.pause();
}
});
request.on('end',function(){
response.writeHead(200);
response.write("\n Content with this url is - \n");
var readStream = fs.createReadStream(query['fileName'],{bufferSize:64*1024});
readStream.on('data',function(chunk){
response.write(chunk.toString());
});
readStream.on('end',function(){
response.write("\n");
response.end();
});
});
writeStream.on('drain',function(){
request.resume();
});
}).listen(8080);
That is changed the flag to the 'a' and it also did not append the data?
Your can use your first variant. But before appendFile() you've opened writeStream for the same query["filename"]. The stream is already opened.
var writeStream = fs.createWriteStream(query['fileName']);
options = {
'flags': 'a' ,
'encoding' : null,
'mode' : 0666
}
var appendStream = fs.createWriteStream(query['fileName'],[options]);
May be it's better to do something like:
var options = {
flags: query.appendFile ? 'w' : 'a'
...
Next: why [options]? You should remove the brackets.
Next: there is no guarantee you'll have filename param in querystring. Please handle this situation.

Resources