Give file link in place of absolute path in fs.createReadStream(); - node.js

I've a code like this
const fileData = fs.createReadStream('Google Sheet Link');
The error I am getting is this.
This is probably due to fs trying to read from an absolute path, rather than directly from the URL.
The path, it is taking is "Directory to which file is located" + "Google sheet link"
I only want to open google sheet link in the createReaderStream Path.
I've hidden the google sheet path due to privacy reasons.

The createReadStream() method is an inbuilt application programming interface of fs module which allow you to open up a file/stream and read the data present in it.
fs.createReadStream( path, options )
Parameters: This method accept two parameters as mentioned above and described below:
path: This parameter holds the path of the file where to read the file. It can be string, buffer or URL.
options: It is an optional parameter that holds string or object.
Now, To avoid paths issues, it's recommended to use path.join, like:
const path = require('path');
const pathGoogleSheet = path.join(__dirname, 'Sheet name');
const readableStream = fs.createReadStream(pathGoogleSheet);
Here, you're creating a path to the google sheet referring to the current directory of the script stored in global variable called __dirname.

Related

In Windows, Node.js' path.join() prepends the current working directory's drive letter(C:\) automatically

I'm using Node.js' path.join() to generate the path in Windows.
const path = require('path');
const myDestPath = path.join('/D', 'test.txt'); // D drive
This results in C:\D\test.txt, prepending the current working directory's drive letter (C:\) automatically, which is not what I want.
How do I go about setting the different drive letter other than C:\ in Node.js?
I've tried path.resolve() as well, but got the same result.
You may need to add extra slash to create path
const path = require('path');
const myDestPath = path.join('D:\\', 'test.txt'); // D drive
console.log(myDestPath)
results in D:\test.txt

Node.js cant import File instance from local folder

I have a problem with importing any file as a file instance from local folder.
Can you help me please?
I can't find anything in 'fs' or 'path', maby because I don't know what to see
I want to get File and pass it to my JS applicatin as a File instance.
Start by using a path relative to your script and the __dirname directive.
fs.readFileSync(__dirname + '/myfile.txt')
You may also wish to lose the / using path to make your code more portable.
const path = require('path')
fs.readFileSync(path.join(__dirname, 'myfile.txt')
In regards to format, the result will be Buffer. If you want a string,
fs.readFileSync(filename,'UTF-8')
If you want a blob, see stackoverflow.com/questions/14653349

Can I pass partial file path in the variable and access in groovy grails?

How Can I pass partial file path in the variable and access in groovy grails?
For-Example-
String path = System.getenv("CATALINA_HOME");
File siteFile = new File("${path}/webapps/ROOT/sitemap.xml")
Please provide the solution so that I can Implement in my project.
You can also get the file using a Relative Path. Just Right click the file that you want to get and then select Copy Relative Path then use that to get the file.
File siteFile = new File("grails-app/assets/images/grails.svg")
Try this one.
String path = System.getProperty( "catalina.base" )
File siteFile = new File("${path}/webapps/ROOT/sitemap.xml")

How to save an image to desktop using TestComplete?

I'm writing tests in JScript in TestComplete. I need to make a screenshot of a web page element, and save it to my desktop as a PNG file.
I tried this code:
var MyPicture = WebPage.SomeLocation.Picture();
MyPicture.SaveToFile("C:\Desktop");
which doesn't seem to be working, and I can't seem to figure out why. My program doesn't crash or anything, it simply doesn't save the picture. What am I doing wrong?
SaveToFile needs a full name of the image to create, including a path. Remember that in JScript you must double the backslashes in paths.
To get the desktop folder path, you can use the SpecialFolders property.
var MyPicture = WebPage.SomeLocation.Picture();
var strImageName = "MyPicture.png";
// Get the Desktop folder path
var strDesktop = Sys.OleObject("WScript.Shell").SpecialFolders("Desktop");
// Build the full path to the image
var strPath = aqFileSystem.IncludeTrailingBackSlash(strDesktop) + strImageName;
MyPicture.SaveToFile(strPath);

How to get file contents from absolute path in Node.js in this code?

I have the following code in NodeJs,
var fs = require('fs');
var data = fs.readFileSync('client/assets/images/car-img.jpg');
This client/assets/images/car-img.jpg is the location of image in system i.e relative path. I want to get file contents from absolute path like this. Consider, getting Google image from this absolute path,
var data = fs.readFileSync('https://www.google.co.in/images/srpr/logo11w.png');
How should I read data using readFileSync? Any help appreciated.

Resources