在 Surge.js 或 Surge客户端 中,下载文件通常可以通过以下方式实现
八方来财全球200+高速节点2026-08-1720
使用 Filesystem.js 你可以使用 Filesystem 来读取和写入文件。 const Filesystem = require('fs'); const Path = require('path'); // 读取文件 const file = new Filesystem().read(Path.from('path/to/file')); // 写入文件 Filesystem.write(path, content, { mode: 'r+' }); 使用 Filesystem.js 的方法 Filesystem 提供了基本的读写功能: const Filesystem = require('fs'); const Path = require('path'); // 读取文件 const file = new Filesystem().read(Path.from('path/to/file')); // 写入文件 Filesystem.write(Path.from('path/to/file'), content, { mode: 'w' }); 使用 os.path 对象 也可以使用 os.path 对象来处理路径: const { join } = require('path'); const path = join(__dirname, 'path/to/file'); const file = new File(path); 处理大文件 如果需要处理大文件(超过 1MB),可以使用缓存或分文件下载: const fs = new Filesystem(); const path = fs.get('/path/to/file'); 处理错误 在下载过程中,可能会遇到错误,可以使用 fs.errorPrompt。 const fs = new Filesystem(); const { error } = fs.errorPrompt('下载失败'); 无论...
使用 Filesystem.js
你可以使用 Filesystem 来读取和写入文件。
const Filesystem = require('fs');
const Path = require('path');
// 读取文件
const file = new Filesystem().read(Path.from('path/to/file'));
// 写入文件
Filesystem.write(path, content, { mode: 'r+' });
使用 Filesystem.js 的方法
Filesystem 提供了基本的读写功能:
const Filesystem = require('fs');
const Path = require('path');
// 读取文件
const file = new Filesystem().read(Path.from('path/to/file'));
// 写入文件
Filesystem.write(Path.from('path/to/file'), content, { mode: 'w' });
使用 os.path 对象
也可以使用 os.path 对象来处理路径:
const { join } = require('path');
const path = join(__dirname, 'path/to/file');
const file = new File(path);
处理大文件
如果需要处理大文件(超过 1MB),可以使用缓存或分文件下载:
const fs = new Filesystem();
const path = fs.get('/path/to/file');
处理错误
在下载过程中,可能会遇到错误,可以使用 fs.errorPrompt。
const fs = new Filesystem();
const { error } = fs.errorPrompt('下载失败');
无论你是想在 Node.js 中使用 Filesystem,还是直接使用 Surge.js,下载文件都是相对简单的步骤,使用 Filesystem.js 或 Path 对象是最常用的方法。

相关文章







