简体   繁体   中英

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.

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.

With the information provided we can do very little, but I'll make a few assumtions:

  • Your "uploads" folder is inside your app folder.
  • The directory structure is very simple and fixed, so you have your app folder and one level below, you have your "uploads" folder.

Then, you can get the full path of those files like this:

//index.js
var filename = "myfile.csv"; ///you already have this one.
var fullpath = __dirname + "/uploads/" + filename;

That's it, by using the __dirname ( see docs here ) variable, you get to fullpath to the index.js file, and from there you can add the rest manually.

Assuming you are using multer with express, try this in your controller method:

var path = require('path');

//gets your app's root path
var root = path.dirname(require.main.filename)

// joins uploaded file path with root. replace filename with your input field name
var absolutePath = path.join(root,req.files['filename'].path) 

Get all files from folder and print each file description.

const path = require( "path" );
const fs = require( 'fs' );
const log = console.log;
const folder = './';

fs.readdirSync( folder ).forEach( file => {
   
   const extname = path.extname( file );
   const filename = path.basename( file, extname );
   const absolutePath = path.resolve( folder, file );

   log( "File : ", file );
   log( "filename : ", filename );
   log( "extname : ", extname );
   log( "absolutePath : ", absolutePath);

});

In TypeScript, I did the following based on the relative file path.

import { resolve } from 'path';

public getValidFileToUpload(): string {
  return resolve('src/assets/validFilePath/testFile.csv');
}

If you are using a "dirent" type:

const path = require( "path" );
full_path = path.resolve( path_to_folder_containing__dir_ent__ , dir_ent.name );

Class: fs.Dirent https://nodejs.org/api/fs.html#fs_dirent_name

Module: fs.path https://nodejs.org/api/path.html

I think the simplest option is: dirname module:https://nodejs.org/docs/latest/api/modules.html#modules_dirname

filename: https://nodejs.org/docs/latest/api/modules.html#modules_filename

console.log(__dirname)

console.log(__filename)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM