Spaces:
Running
Running
Fix file select to fall back to old file API
Browse files- index.html +24 -17
index.html
CHANGED
|
@@ -139,29 +139,36 @@
|
|
| 139 |
|
| 140 |
<script>
|
| 141 |
window.testImages = [];
|
| 142 |
-
|
| 143 |
-
// At the top of your script, declare these variables
|
| 144 |
let imageHandles = [];
|
| 145 |
|
| 146 |
-
|
| 147 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 148 |
try {
|
| 149 |
-
|
| 150 |
multiple: true,
|
| 151 |
-
types: [{
|
| 152 |
-
description: 'Images',
|
| 153 |
-
accept: {
|
| 154 |
-
'image/*': ['.png', '.gif', '.jpeg', '.jpg', '.webp', '.avif', '.svg']
|
| 155 |
-
}
|
| 156 |
-
}]
|
| 157 |
});
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
await loadImagesFromHandles();
|
| 162 |
-
} catch (err) {
|
| 163 |
-
console.error("Error selecting files:", err);
|
| 164 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 165 |
});
|
| 166 |
|
| 167 |
async function loadImagesFromHandles() {
|
|
|
|
| 139 |
|
| 140 |
<script>
|
| 141 |
window.testImages = [];
|
|
|
|
|
|
|
| 142 |
let imageHandles = [];
|
| 143 |
|
| 144 |
+
const fileInput = Object.assign(document.createElement('input'), {
|
| 145 |
+
type: 'file', multiple: true,
|
| 146 |
+
accept: '.png,.gif,.jpeg,.jpg,.webp,.avif,.svg,image/*',
|
| 147 |
+
style: 'display:none',
|
| 148 |
+
});
|
| 149 |
+
document.body.appendChild(fileInput);
|
| 150 |
+
|
| 151 |
+
document.getElementById('imageLoader').addEventListener('click', async () => {
|
| 152 |
try {
|
| 153 |
+
imageHandles = await window.showOpenFilePicker({
|
| 154 |
multiple: true,
|
| 155 |
+
types: [{ description: 'Images', accept: { 'image/*': ['.png','.gif','.jpeg','.jpg','.webp','.avif','.svg'] }}]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 156 |
});
|
| 157 |
+
} catch {
|
| 158 |
+
fileInput.click();
|
| 159 |
+
return;
|
|
|
|
|
|
|
|
|
|
| 160 |
}
|
| 161 |
+
let { set } = await import('https://unpkg.com/[email protected]/dist/esm/index.js');
|
| 162 |
+
await set('imageHandles', imageHandles);
|
| 163 |
+
await loadImagesFromHandles();
|
| 164 |
+
});
|
| 165 |
+
|
| 166 |
+
fileInput.addEventListener('change', async ({ target: { files } }) => {
|
| 167 |
+
if (!files.length) return;
|
| 168 |
+
imageHandles = Array.from(files).map(file => ({ getFile: async () => file, kind: 'file', name: file.name }));
|
| 169 |
+
let { set } = await import('https://unpkg.com/[email protected]/dist/esm/index.js');
|
| 170 |
+
await set('imageHandles', imageHandles);
|
| 171 |
+
await loadImagesFromHandles();
|
| 172 |
});
|
| 173 |
|
| 174 |
async function loadImagesFromHandles() {
|