You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Express res.download从文件共享获取文件遇路径问题求助

Fixing Express res.download() Failure with Network File Shares

Hey there, let's work through this issue you're facing where Express can't find files on your network share even though you can access it from Mac and Windows. The core problem here is that Node.js's built-in file system (fs) module — which res.download() relies on — doesn't natively understand UNC paths like //${ipAddressOfServer}/ParentFolder/ChildFolder/${fileName}. Here are two solid solutions to get this working:

Option 1: Mount the Network Share to a Local Directory

The simplest approach is to map the network share to a local folder on your server running Express. This way, Node.js can treat it like any other local file path.

For macOS/Linux:

Use the mount_smbfs command to mount the share:

# Replace placeholders with your actual credentials and paths
mount_smbfs //your-username:your-password@${ipAddressOfServer}/ParentFolder /path/to/local/mount

Once mounted, update your Express code to use the local mount path instead of the UNC path:

const pathToFile = `/path/to/local/mount/ChildFolder/${fileName}`;
res.download(pathToFile, fileName, (err) => {
  if (err) {
    console.error(err);
    res.status(500).send('Failed to download file');
  }
});

For Windows:

Use the net use command to assign a drive letter to the share:

net use Z: \\${ipAddressOfServer}\ParentFolder /user:your-username your-password

Then use the drive letter in your code:

const pathToFile = `Z:\\ChildFolder\\${fileName}`;
res.download(pathToFile, fileName, (err) => {
  if (err) {
    console.error(err);
    res.status(500).send('Failed to download file');
  }
});

Option 2: Use a Third-Party SMB/CIFS Library

If mounting isn't feasible (e.g., you need dynamic access to multiple shares), use an SMB client library to directly interact with the network share without mounting. We'll use @marsaud/smb2 (a popular, maintained library):

  1. First install the package:
npm install @marsaud/smb2
  1. Update your Express route to use the SMB client instead of res.download():
const SMB2 = require('@marsaud/smb2');

// Initialize the SMB client with your share details
const smbClient = new SMB2({
  share: `\\\\${ipAddressOfServer}\\ParentFolder`,
  username: 'your-username', // Required if the share is secured
  password: 'your-password',
  // Add domain if your network uses one: domain: 'your-domain'
});

app.get('/download-file', (req, res) => {
  const fileName = req.query.fileName; // Adjust based on how you get the filename
  const remoteFilePath = `ChildFolder/${fileName}`;

  // Create a read stream from the SMB share and pipe it to the response
  smbClient.createReadStream(remoteFilePath)
    .on('error', (err) => {
      console.error('SMB access error:', err);
      res.status(500).send(`Could not access file: ${err.message}`);
    })
    .pipe(res.attachment(fileName)); // Sets the correct download headers
});

Quick Check: Path Escaping

One last thing to verify: if you're using Windows, UNC paths in JavaScript strings need double backslashes (since backslashes are escape characters). So \\${ipAddressOfServer}\\ParentFolder instead of //${ipAddressOfServer}/ParentFolder — though mounting or using the SMB library avoids this headache entirely.

内容的提问来源于stack exchange,提问作者teej

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.08 20:52:40