Skip to content

[Bug] 上传文件到中文目录时,会创建 URL 编码格式的乱码文件夹 #3

@LeNotFound

Description

@LeNotFound

描述 (Description)
当尝试将文件上传到包含非 ASCII 字符(如中文)的目录时,服务器未对 URL 参数中的路径进行解码。导致系统没有使用现有的中文目录,而是在文件系统中创建了一个名为 URL 编码字符串(如 %E6%9D%82%E9%A1%B9)的新目录。

复现步骤 (Steps to Reproduce)

  1. files 目录下存在一个中文文件夹,例如 files/杂项
  2. 在浏览器中进入该文件夹,URL 显示为 .../files/%E6%9D%82%E9%A1%B9/
  3. 拖拽一个文件上传到该目录。
  4. 检查服务器的 files 目录,发现多了一个名为 %E6%9D%82%E9%A1%B9 的文件夹,文件被上传到了这里,而不是原有的 files/杂项 目录。

原因分析 (Root Cause)
index.js 处理 /upload 请求时,直接从 URL 查询参数 dir 中获取路径,但没有进行 decodeURIComponent 解码。
前端浏览器通常会将 URL 中的中文路径自动编码传输,Node.js 接收到的是编码后的字符串,随后直接用作文件系统路径创建目录。

相关代码 (Code Snippet)
index.js 约第 36 行:

// 当前代码
let uploadDir = urlObj.searchParams.get('dir') || '/';
// uploadDir 此时可能为 "/%E6%9D%82%E9%A1%B9/"
uploadDir = path.join(FILES_DIR, uploadDir); 

建议方案 (Proposed Solution)
在获取 uploadDir 后,立即对其进行解码。

// 建议修改
let uploadDir = urlObj.searchParams.get('dir') || '/';
uploadDir = decodeURIComponent(uploadDir); // 添加解码步骤
uploadDir = path.join(FILES_DIR, uploadDir);

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