[英]Get full file path in Node.js
I have an application which uploads csv file into a particular folder, say, "uploads".我有一个应用程序将 csv 文件上传到特定文件夹,例如“上传”。 Now I want to get the full path of that csv file, for example, D:\MyNodeApp\uploads\Test.csv
.现在我想获取 csv 文件的完整路径,例如D:\MyNodeApp\uploads\Test.csv
。
How do I get the file location in Node.js?如何获取 Node.js 中的文件位置? I used multer to upload file.我用multer上传文件。
var path = require("path");
var absolutePath = path.resolve("Relative file path");
You dir structure for example:你的目录结构例如:
C:->WebServer->Public->Uploads->MyFile.csv C:->WebServer->Public->Uploads->MyFile.csv
and your working directory would be Public for example, path.resolve would be like that.例如,您的工作目录将是 Public,path.resolve 就是这样。
path.resolve("./Uploads/MyFile.csv");
POSIX home/WebServer/Public/Uploads/MyFile.csv POSIX 主页/WebServer/Public/Uploads/MyFile.csv
WINDOWS C:\\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.该解决方案是多平台的,允许您的应用程序在 windows 和 posix 机器上运行。
With the information provided we can do very little, but I'll make a few assumtions: 根据所提供的信息,我们可以做的很少,但我会做一些补充:
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. 就是这样,通过使用__dirname
( 请参阅此处的文档 )变量,您可以获得index.js
文件的完整路径,然后您可以手动添加其余文件。
Assuming you are using multer with express, try this in your controller method:假设您正在将 multer 与 express 一起使用,请在您的控制器方法中尝试此操作:
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)
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.在 TypeScript 中,我根据相对文件路径执行了以下操作。
import { resolve } from 'path';
public getValidFileToUpload(): string {
return resolve('src/assets/validFilePath/testFile.csv');
}
If you are using a "dirent" type:如果您使用的是“dirent”类型:
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类:fs.Dirent https://nodejs.org/api/fs.html#fs_dirent_name
Module: fs.path https://nodejs.org/api/path.html模块: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我认为最简单的选择是:目录名模块:https://nodejs.org/docs/latest/api/modules.html#modules_dirname
filename: https://nodejs.org/docs/latest/api/modules.html#modules_filename文件名: https://nodejs.org/docs/latest/api/modules.html#modules_filename
console.log(__dirname) console.log(__dirname)
console.log(__filename) console.log(__filename)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.