Skip to content

Commit daf6661

Browse files
committed
Use showSaveFilePicker for native Save As dialog
1 parent fc1d52f commit daf6661

1 file changed

Lines changed: 22 additions & 11 deletions

File tree

web/index.html

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2760,22 +2760,33 @@ <h1>C.O.R.A</h1>
27602760
}
27612761

27622762
async function downloadStatsServer() {
2763-
// Fetch and download stats_server.py as a file
2763+
// Fetch file and show native Save As dialog
27642764
try {
27652765
const response = await fetch('https://raw.githubusercontent.com/Unity-Lab-AI/CORA/main/services/stats_server.py');
27662766
if (!response.ok) throw new Error('Failed to fetch');
27672767
const blob = await response.blob();
2768-
const url = window.URL.createObjectURL(blob);
2769-
const a = document.createElement('a');
2770-
a.href = url;
2771-
a.download = 'stats_server.py';
2772-
document.body.appendChild(a);
2773-
a.click();
2774-
document.body.removeChild(a);
2775-
window.URL.revokeObjectURL(url);
2768+
2769+
// Use File System Access API for native Save As dialog
2770+
if (window.showSaveFilePicker) {
2771+
const handle = await window.showSaveFilePicker({
2772+
suggestedName: 'stats_server.py',
2773+
types: [{ description: 'Python File', accept: { 'text/x-python': ['.py'] } }]
2774+
});
2775+
const writable = await handle.createWritable();
2776+
await writable.write(blob);
2777+
await writable.close();
2778+
} else {
2779+
// Fallback for browsers without File System Access API
2780+
const url = window.URL.createObjectURL(blob);
2781+
const a = document.createElement('a');
2782+
a.href = url;
2783+
a.download = 'stats_server.py';
2784+
a.click();
2785+
window.URL.revokeObjectURL(url);
2786+
}
27762787
} catch (e) {
2777-
// Fallback: open in new tab so user can save manually
2778-
alert('Auto-download failed. Opening file in new tab - right-click and "Save As"');
2788+
if (e.name === 'AbortError') return; // User cancelled save dialog
2789+
alert('Download failed. Opening file in new tab - right-click and "Save As"');
27792790
window.open('https://raw.githubusercontent.com/Unity-Lab-AI/CORA/main/services/stats_server.py', '_blank');
27802791
}
27812792
}

0 commit comments

Comments
 (0)