Shift2App
Home chevron_right File Handling
folder

File Handling

Downloads, camera, and file uploads

Downloads

Download files to device

Shift2App automatically handles file downloads. When a user clicks a download link, the file is downloaded to the device's Downloads folder.

check_circle
Automatic Handling

Download links work automatically - no JavaScript needed.

shift2appjs.downloadFile(url, filename)

Programmatically download a file.

Parameters
url String - URL of file to download
filename String - Suggested filename
Returns

void

javascript
shift2appjs.downloadFile("https://example.com/report.pdf", "monthly-report.pdf");

Camera & Gallery

Capture photos or select from gallery

warning
Permission Required

Camera must be enabled in app configuration.

html
<!-- Photo input - shows camera/gallery chooser -->
<input type="file" accept="image/*" capture="environment" id="photoInput">

<!-- Camera only -->
<input type="file" accept="image/*" capture="camera">

<!-- Gallery only -->
<input type="file" accept="image/*">

<script>
document.getElementById("photoInput").addEventListener("change", (e) => {
    const file = e.target.files[0];
    if (file) {
        // Preview
        const reader = new FileReader();
        reader.onload = (e) => {
            document.getElementById("preview").src = e.target.result;
        };
        reader.readAsDataURL(file);
    }
});
</script>

File Uploads

Upload files to server

html
<!-- Any file type -->
<input type="file" id="fileInput">

<!-- Specific types -->
<input type="file" accept=".pdf,.doc,.docx">
<input type="file" accept="video/*">

<script>
async function uploadFile(file) {
    const formData = new FormData();
    formData.append("file", file);
    
    // Show progress
    shift2appjs.showToast("Uploading...");
    
    const response = await fetch("/api/upload", {
        method: "POST",
        body: formData
    });
    
    if (response.ok) {
        shift2appjs.showToast("Upload complete!");
    } else {
        shift2appjs.showToast("Upload failed");
    }
}

document.getElementById("fileInput").addEventListener("change", (e) => {
    if (e.target.files[0]) {
        uploadFile(e.target.files[0]);
    }
});
</script>
dashboard Dashboard menu_book Docs science Playground