Drag and drop OS files into Webview #2820
Replies: 1 comment
-
|
Hello! The shift-key requirement for OS file drag and drop into webviews is indeed a security limitation in VS Code. Here are several effective alternatives: Option 1: File Input with Custom Styling <label class="drop-zone">
<input type="file" multiple style="display: none;" id="fileInput">
<div>Drop files here or click to browse</div>
</label>
<script>
const fileInput = document.getElementById('fileInput');
fileInput.addEventListener('change', (e) => {
const files = Array.from(e.target.files);
files.forEach(file => handleFile(file));
});
</script>Option 2: Enhanced Drag Detection with User Guidance document.addEventListener('dragover', (e) => {
e.preventDefault();
if (e.dataTransfer.types.includes('Files')) {
showShiftHint();
}
});
document.addEventListener('drop', (e) => {
e.preventDefault();
if (!e.shiftKey) {
showShiftRequiredMessage();
return;
}
const files = Array.from(e.dataTransfer.files);
handleFiles(files);
});Option 3: VS Code Native File Dialog const uris = await vscode.window.showOpenDialog({
canSelectMany: true,
openLabel: 'Select Files',
filters: { 'All Files': ['*'] }
});
if (uris) {
webview.postMessage({
type: 'filesSelected',
files: uris.map(uri => uri.fsPath)
});
}Option 4: Mixed Approach with Clear UX
The shift-key requirement is intentional for security, so designing around this limitation with clear user interfaces will provide the best experience for your extension users. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I have a custom view (Webview) in a sidebar (WebviewView). I'd like the custom view to be able to handle drag and drop events, specifically file drag and drop (from the OS, ie file explorer).
I've implemented the standard HTML5 events, but these only seem to work when holding down shift. I'm curious if this is intentional or not -- it makes the functionality effectively impossible to discover.
Are there any alternatives to this?
Beta Was this translation helpful? Give feedback.
All reactions