gpt4 book ai didi

node.js - 发送文件绝对路径

转载 作者:太空宇宙 更新时间:2023-11-04 02:29:07 24 4
gpt4 key购买 nike

在 Windows 计算机上,我有一个映射共享文件夹 => Z:/uploads/

使用express,我使用res.senFile将文件返回到浏览器:

var download = config.file_dir + "/" + file;
res.sendFile(download);

下载值为Z:/uploads/737237213791239.pdf

我收到此错误:

throw new TypeError('path must be absolute or specify root to res.sendFile

我给出的是绝对路径吗?

最佳答案

我遇到了同样的问题。我发现用正斜杠配置我的文件夹是最简单的,使用 path.join 来发送。这应该适用于驱动器号或 UNC 路径。

var path = require("path");
config.file_dir = "z:/folder";
//use forward slashes for UNC if you wish to use that instead ie //server/share

var file = path.join(config.file_dir, urlPath);

res.sendFile(file, (err) => {
if (err) {
res.status(err.status || 500).send();
}
});

关于node.js - 发送文件绝对路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28383774/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com