downloadFromUrl
Category: File Operations
Namespace: web.files.downloadFromUrl
Description
Download files directly from URLs to disk without manual base64 conversion. This API provides a simple, efficient way to fetch remote files and save them locally with automatic error handling and progress tracking.
Replaces: The old pattern of using fetch() + base64 conversion + web.files.download() (reduces code by 95%)
Syntax
const result = await web.files.downloadFromUrl({
url: string,
filePath: string,
overwrite: boolean,
timeout: number,
headers: object
});
Parameters
- url (required) - HTTP/HTTPS URL to download from
- filePath (required) - Full destination path including filename
- overwrite (optional) - Overwrite existing file without prompt (default: false)
- timeout (optional) - Timeout in milliseconds (default: 30000)
- headers (optional) - Custom HTTP request headers object (default:
{})
Returns
Promise<Object> - Result object:
{
success: boolean,
filePath?: string,
size?: number,
contentType?: string,
error?: string
}
Example
// Simple download
const result = await web.files.downloadFromUrl({
url: 'https://cdn.com/library.js',
filePath: 'C:\\MyApp\\scripts\\library.js',
overwrite: true
});
if (result.success) {
console.log(`Downloaded ${result.size} bytes`);
}
// With custom timeout
const result = await web.files.downloadFromUrl({
url: 'https://example.com/large-file.zip',
filePath: 'C:\\Downloads\\large-file.zip',
overwrite: true,
timeout: 60000 // 60 seconds for large files
});
//Or a reusable version:
async function downloadToFile(url, destination) {
const result = await web.files.downloadFromUrl({
url: url,
filePath: destination,
overwrite: true,
timeout: 60000
});
return result.success ? result.filePath : null;
}
// Usage
const file = await downloadToFile(
'https://example.com/file.zip',
'C:\\Downloads\\file.zip'
);
Use Cases
- Download CDN resources (fonts, libraries, stylesheets)
- Fetch remote data files
- Download images and media files
- Sync content from servers
- Download API responses to disk
Benefits
- 95% less code - One simple call vs 30+ lines
- More efficient - Streams directly to disk, no memory bloat
- No base64 overhead - Eliminates 33% size increase
- Better error handling - HTTP status codes and detailed messages
- Automatic cleanup - Partial files deleted on failure