Skip to content

[Bug] 上传包含 # 字符的文件后,文件链接访问报 404 #2

@LeNotFound

Description

@LeNotFound

描述 (Description)
当上传的文件名中包含井号 #(例如 Video #1.mp4)时,文件可以成功上传到服务器的 files 目录。但是在前端文件列表中点击该文件时,浏览器会报 404 Not Found 错误。

复现步骤 (Steps to Reproduce)

  1. 启动服务器 (node index.js)。
  2. 拖拽上传一个文件名包含 # 的文件,例如 test#file.txt
  3. 上传成功后,在页面文件列表中点击该文件。
  4. 观察到浏览器跳转并在服务器端返回 404。

原因分析 (Root Cause)
URL 中的 # 字符在浏览器中被解析为片段标识符(Fragment/Anchor)。
例如链接 http://localhost:3000/test#file.txt

  • 浏览器请求的 URLhttp://localhost:3000/test
  • 服务器查找的文件files/test
  • 实际存在的文件files/test#file.txt

由于请求路径被 # 截断,服务器找不到对应的文件,因此返回 404。

建议方案 (Proposed Solution)
修改 index.js 中的上传处理逻辑,在保存文件前对文件名进行清洗,将 # 替换为下划线 _ 或其他安全字符。

代码修改示例 (Code Snippet)
index.js/upload 接口处理逻辑中:

// Current index.js logic
let filename = decodeURIComponent(req.headers['x-filename'] || 'upload.bin');

// Proposed change
filename = filename.replace(/#/g, '_'); // Replace '#' with '_'

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions