简体   繁体   English

节点将相对路径转换为绝对路径,不需要

[英]Node converting relative path to absolute, not desired

I have the following issue, in the function sendDownload(downlodable,objPathArray, responseObject) I am receiving the parameter objPathArray like this:我有以下问题,在 function sendDownload(downlodable,objPathArray, responseObject) 我收到这样的参数 objPathArray:

[{"pathToFile":"./REPORTS/portfolio/onDemand/Portfolio_report_HP_17.08.2021.xlsx","file":"Portfolio_report_HP_17.08.2021.xlsx"}]

The function: function:

function sendDownload(downlodable ,objPathArray, responseObject) {    
    if (downlodable) {  
        responseObject.download((objPathArray[0].pathToFile), (objPathArray[0].file))      
        console.log('HERE ' + JSON.stringify(objPathArray))        
    }
}

but when I call the function in my app I receive this error Error: ENOENT: no such file or directory, stat 'C:\Work\reporting-server\REPORTS\portfolio\onDemand\Portfolio_report_HP_17.08.2021.xlsx' and that's because Node is changing my relative path for the file in an absolute one.但是当我在我的应用程序中调用 function 时,我收到此错误Error: ENOENT: no such file or directory, stat 'C:\Work\reporting-server\REPORTS\portfolio\onDemand\Portfolio_report_HP_17.08.2021.xlsx'那是因为 Node正在以绝对路径更改文件的相对路径。 What is the best option for me in order to solve this issue?为了解决这个问题,对我来说最好的选择是什么?

Thank you~谢谢~

Node.js must resolve the path to find the file. Node.js必须解析找到文件的路径。

If you give it a relative path then it has to start somewhere and that somewhere is the current working directory.如果你给它一个相对路径,那么它必须从某个地方开始,而那个地方就是当前的工作目录。

If you want it to read the file from somewhere else then resolve it to an absolute path yourself.如果您希望它从其他地方读取文件,请自行将其解析为绝对路径。

Generally speaking the tools to do this will be path.resolve and something to tell it where to start from such as a configuration file , environment variable , orthe directory the module is in .一般来说,执行此操作的工具是path.resolve和一些告诉它从哪里开始的东西,例如配置文件环境变量模块所在的目录

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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