question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Question] How to drag and drop files

See original GitHub issue

How do I write a drag and drop action with a file?

There’s an example in the docs that I don’t understand

// Note you can only create DataTransfer in Chromium and Firefox
const dataTransfer = await page.evaluateHandle(() => new DataTransfer());
await element.dispatchEvent('dragstart', { dataTransfer });

How do I select a file to drag and drop?

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:1
  • Comments:8 (1 by maintainers)

github_iconTop GitHub Comments

10reactions
CharlieDigitalcommented, Dec 21, 2021

Got it.

// Read your file into a buffer.
const buffer = readFileSync('./runtime_config/common/file.pdf');

// Create the DataTransfer and File
const dataTransfer = await scope.page.evaluateHandle((data) => {
    const dt = new DataTransfer();
    // Convert the buffer to a hex array
    const file = new File([data.toString('hex')], 'file.pdf', { type: 'application/pdf' });
    dt.items.add(file);
    return dt;
}, buffer);

// Now dispatch
await page.dispatchEvent('YOUR_TARGET_SELECTOR', 'drop', { dataTransfer });

If you’re in TypeScript, add this at the top of your file:

/// <reference lib="dom"/>
1reaction
Jeromearsenecommented, Jun 15, 2022

I purpose this for more generic approach.

Read more comments on GitHub >

github_iconTop Results From Across the Web

3 Ways to Drag and Drop - wikiHow
1. Turn on your computer. Decide the files that you want to move to a new location. Choose where that new location will...
Read more >
[Question]: How to drag and drop a file ? · Issue #1410 - GitHub
I have to drag a file stored in system to the html upload section. In this scenario, there is only target. Source is...
Read more >
Drag and Drop Does Not Work at Windows 11 - Microsoft Learn
Quickly drag and drop files between app windows, by hovering over apps in the taskbar to bring their windows to the foreground. Try...
Read more >
Drag and drop questions - Presenter - Adobe Support
Drag-and-drop is supported only in the .pptx file format and not in .ppt file format. In the Adobe Presenter ribbon, click Manage >...
Read more >
c# - How do I drag and drop files into an application?
Shouldn't the class be a sealed class to avoid making a virtual call on the this.AllowDrop ? stackoverflow.com/questions/119506/… – CJBS. Feb 10, 2014...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found