File Downloader

Download and Share Files using the Native Modal

The Despia Native File Download SDK enables your application to trigger native file downloads directly in the mobile app using just window.location.href. This is the simplest possible implementation requiring only one line of code.

Quick Start

1. Set up the file URL

Prepare the direct URL to the file you want the user to download.

const fileUrl = "https://example.com/path/to/your/file.pdf";

2. Trigger the native download

Use window.location.href to redirect the browser to the file URL. This single line is all you need!

window.location.href = fileUrl;

3. That's it!

There's no need for step 3. The download will start automatically.

How It Works

One-Line Download Mechanism

When you assign a file URL to window.location.href, the browser navigates to that URL. If the file type is one the browser can't display natively (like .zip or .pdf depending on settings), it will automatically trigger the browser's download mechanism. No callbacks, events, or additional code needed.

Content-Disposition Headers

This approach works best when the server responds with appropriate Content-Disposition headers that indicate the file should be downloaded rather than displayed inline.

Implementation Examples

Here's how you can implement native file downloads in various scenarios:

// Basic implementation example - this is all you need!
window.location.href = "https://example.com/path/to/your/file.pdf";

// Alternative direct implementation
window.location.href = fileUrl;

// For NO-CODE/LOW-CODE TOOLS - just use a custom code block / action to run this one line:
// WEWEB / WIZED / NORDCRAFT / TODDLE - just add this single line in a code block or action:
window.location.href = "https://example.com/path/to/your/file.pdf";

Best Practices

Security

  • Always ensure file URLs point to trusted sources

  • Consider implementing content security policies

  • For sensitive files, include authentication tokens in URLs or implement proper authentication checks

User Experience

  • Provide clear feedback to users that a download is starting

  • Consider opening downloads in new tabs for larger files

  • Implement progress indicators for large file downloads

Performance

  • Avoid triggering multiple downloads simultaneously

  • Consider using blob URLs for dynamically generated content

  • Pre-validate file existence before attempting download

Complete Implementation Example

Here's a complete example showing how to implement file downloads with user feedback:

The browser-native alert() JavaScript method is automatically transformed into the native iOS/Android alert by Despia’s Runtime.

function initiateDownload(fileUrl) {
  try {
    // Show loading indicator (if you have a UI function for this)
    showLoadingIndicator("Preparing your download...");
    
    // Optional: Log download attempt for analytics
    console.log("Download initiated:", fileUrl);
    
    // Display a message to the user
    alert("Your download will begin momentarily. Please wait...");
    
    // Trigger the download with window.location.href - this is the key part!
    window.location.href = fileUrl;
    
    // Note: Any code after window.location.href may not execute
    // if the browser immediately starts navigating to the new URL
    
  } catch (error) {
    // Error handling
    console.error("Download failed:", error);
    alert("Download failed. Please try again or contact support.");
  }
}

Troubleshooting

Common Issues

Download Doesn't Start

  • Ensure the file URL is correct and accessible

  • Check if the file server has proper CORS headers set

  • Verify Content-Disposition headers are set correctly on the server

  • Make sure the URL contains the full path including http:// or https://

File Opens in Browser Instead of Downloading

  • Ensure the server sets the correct Content-Type headers

  • Some file types (like PDFs) may open in the browser by default - this depends on browser settings

Security Errors

  • Make sure URLs use HTTPS for secure downloads

  • Check that the file domain matches your application domain or has proper CORS headers

  • Avoid downloading files from untrusted sources

Need Help?

For additional support or questions, please contact our support team at support@despia.com

Updated on