Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get full file path in Node.js

I have an application which uploads csv file into a particular folder, say, "uploads". Now I want to get the full path of that csv file, for example, D:\MyNodeApp\uploads\Test.csv.

How do I get the file location in Node.js? I used multer to upload file.

like image 250
ImCoder Avatar asked Jul 09 '15 12:07

ImCoder


People also ask

How do I get the full path in node JS?

We can get the path of the present script in node. js by using __dirname and __filename module scope variables. __dirname: It returns the directory name of the current module in which the current script is located.

How do I find the path of a node?

The first method to get the path of the current directory is the __dirname method. This is a Node. js core module that gets the current path of whatever directory the JavaScript file or module is running in. And you can also get the same result by using the path.

How can I get absolute path of a file using JavaScript?

Use the path. resolve() method to get an absolute path of a file from a relative path in Node. js, e.g. path. resolve('./some-file.


1 Answers

var path = require("path"); var absolutePath = path.resolve("Relative file path"); 

You dir structure for example:

C:->WebServer->Public->Uploads->MyFile.csv

and your working directory would be Public for example, path.resolve would be like that.

path.resolve("./Uploads/MyFile.csv"); 

POSIX home/WebServer/Public/Uploads/MyFile.csv
WINDOWS C:\WebServer\Public\Uploads\MyFile.csv

this solution is multiplatform and allows your application to work on both windows and posix machines.

like image 142
Anton Stafeyev Avatar answered Sep 28 '22 11:09

Anton Stafeyev