Javascript 在 node.js 中获取文件完整路径
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31317007/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get a file full path in node.js
提问by ImCoder
I have a application which uploads csv file into a particular folder say uploads. Now I want to get the full path of that csv file. For e.g. D:\MyNodeApp\uploads\Test.csv
我有一个应用程序将 csv 文件上传到一个特定的文件夹,比如上传。现在我想获取该 csv 文件的完整路径。例如 D:\MyNodeApp\uploads\Test.csv
How to I get the file location in node.js? I used multer to file upload.
如何获取 node.js 中的文件位置?我用multer上传文件。
Thanks in advance
提前致谢
采纳答案by Deleteman
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.
- 您的“上传”文件夹位于您的应用程序文件夹中。
- 目录结构非常简单和固定,所以你有你的 app 文件夹,下一层,你有你的“上传”文件夹。
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
文件的完整路径,然后您可以手动添加其余部分。
回答by Anton Stafeyev
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
WINDOWS C:\WebServer\Public\Uploads\MyFile.csv
POSIX 主页/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 机器上运行。
回答by hassansin
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)
回答by Alferd Nobel
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');
}
回答by M. Hamza Rajput
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( testFolder, file );
log( "File : ", file );
log( "filename : ", filename );
log( "extname : ", extname );
log( "absolutePath : ", absolutePath);
});