New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How can I get info about dragging files in canDrop method? #584
Comments
Are you looking for not allowing drag on the Source OR to show not allowed on the Target based on a type ?
|
@theTechie No, I'm loking for a way of reading file extension that I'm currently dragging. |
All of the source and target methods get You can use Reference:
|
It doesn't work for files |
That's strange. I have done a similar thing and it seems to be working for me. If you can share some code, we can probably see what's wrong. |
Could you please show how you read file extension in canDrop method? |
as i already mentioned above, |
Read my fist comment, when you read files in canDrop it throws error like I mentioned. Do you have working example of reading files in canDrop or you just refer to the documentation? Error says that i can't read files until drop event NativeDragSources.js:61 Browser doesn't allow reading "files" until the drop event |
It's possible we are using two different versions and this restriction has been added later. I am using 2.1.4. |
Ok, I will test the latest version and let you know if it works |
I'm getting this, too, when trying to filter files in |
According to MDN my case, at least, could be accomplished using a property on |
No, I use the same version. Also updated to the latest possible version (2.2.4) and getting the same error: canDrop method looks like this: canDrop: function (props, monitor) {
let files = monitor.getItem().files;
console.log('Files: ', files);
return true;
} How can it work on your side? Do you have working example? |
I will share a working example shortly.
On Mon, 6 Mar 2017 at 4:12 PM, Andrey Karavaychik ***@***.***> wrote:
@theTechie <https://github.com/theTechie>
It's possible we are using two different versions and this restriction has
been added later. I am using 2.1.4.
No, I use the same version. Also updated to the latest possible version
(2.2.4) and getting the same error:
[image: image]
<https://cloud.githubusercontent.com/assets/710513/23606619/8acabe7a-0272-11e7-8db1-b1c88870cf92.png>
canDrop method looks like this:
canDrop: function (props, monitor) {
let files = monitor.getItem().files;
console.log('Files: ', files);
return true;
}
How can it work on your side? Do you have working example?
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#584 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AA_31panjV9Uo11jF63f2Pkx4FBKiV6Aks5ri-MtgaJpZM4K5hkG>
.
--
Regards,
Gagan
|
@andrewQwer - really sorry! sorry for misleading! PS: i am looking into ways we can achieve this, will let you know if i am able to. |
I don't think this will ever be possible. Looks like reading info on dragged items is restricted intentionally for security purpose. |
Would it be possible to expose some information about the files via the |
This might be something to be added to the HTML5 backend, but it does seem possible to determine if a file fits an accepted set of mime types before the file/folder is dropped. Specifcally, react-dropzone does this when you hover the item over the dropzone, as can be seen in this example. |
Any solution for this issue yet? |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
Not stale |
I'm also looking for a way to allow dropping specific files, e.g.: images, video, like react-dropzone. Does anyone have a solution? |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
I have the issue too. It seems to be quite hardcoded in the "NativeDragSource.ts". It creates a DragSource, however, for accessing the values it just adds a console-warning mentioning that the browser wouldn´t allow it. However, the browser DOES allow it, it is just in protected mode and does not allow changing it or accessing the DATA ( of the files, but the items can with the files´ properties should be able to be enumerated. (like DropZone does). Will try something and come back to you |
@LeopoldLerch @darthtrevino I still cannot access to |
Thing is, the events, that the browser delivers, are as they are, we can´t change them. The browser gives access to the data only on dragstart. After that, all subsequent events are in protected mode, which means, you can´t access the data. if you use the onHover method of useDrop, you will get empty metadata, as the browser does not allow you to access it on "dragenter". Even if you store the object in dragstart (or the collect-function), the browser will in the background clear the properties, as you do have a reference to the object, not a copy of it. The only way to work around that, is to use the "collect" function. Here you can access the "items" or "files" of the item (depending on the browser you use, IE11 will never allow you to access them before ondrop). however, you have to do all by yourself: validating and remembering it in eg. a useRef. For example
|
@LeopoldLerch |
@LeopoldLerch This does not work for me on Chrome 91 or Safari 14.1.1 on macOS. Surprisingly it does work on Firefox 90. In Chrome and Safari I also second that getting this information is certainly possible in all the browsers as pointed out in #584 (comment). Go to https://react-dropzone.js.org/#section-styling-dropzone and try to drop an image and the border will turn green. Try to drop a different file and it will turn red. This is consistent across Chrome, Safari, and Firefox so it seems that Test case of |
This is needed to load DataTransferItems from subsequent events in Chrome and Safari. Fixes react-dnd#584
Chrome (and apparently Safari too) will empty the `DataTransferItemList` after a given drag event is done so we need to point `NativeDragSource` to a new list on each dragenter/dragover event. See https://bugs.chromium.org/p/chromium/issues/detail?id=137231 Fixes react-dnd#584
Proposed fix and explanation of why it's not fully working right now: #3262 |
@LeopoldLerch Thanks for the first fix you submitted! I can see now what you meant too. I guess your solution is to only use the data when the |
@LeopoldLerch |
@thediamondor Make sure you're actually making a copy of the data, or storing only primitive values, e.g. store the interface FilesItem {
files: File[];
items: DataTransferItemList;
}
function getFileTypes(item: FilesItem | null): string[] {
if (!item) {
return [];
}
const { files, items } = item;
if (files && files.length > 0) {
return files.map((f) => f.type);
}
if (items && items.length > 0) {
return Array.from(items).map((i) => i.type);
}
return [];
}
// ...
collect: (monitor) => {
const item = monitor.getItem() as FilesItem | null;
const types = getFileTypes(item);
// Only some events will propagate correct file info due to a bug in
// react-dnd so don't change state if there's no data.
// See https://github.com/react-dnd/react-dnd/pull/3262
if (types.length !== 0) {
if (types.length > 1) {
setState('wrongCount');
} else if (types[0] !== accept) {
setState('wrongType');
} else {
setState('correct');
}
}
return {
isOver: monitor.isOver(),
};
}, |
My problem is that I'm trying to get the names of the files, and not the type. But unfortunately if I call getAsFile on the items the result is null |
@thediamondor That's not possible until the drop happens. I believe See https://html.spec.whatwg.org/multipage/dnd.html#dndevents and https://stackoverflow.com/a/41451395/365238 |
Is there any way around this protection? |
You have to read out the relevant information when the objects are not in protected mode, that is eg when Drag starts. Thats why my Code checks if it can access the items (this is only the case, when the drag actually started), and when, it reads out relevant informations or in my case, checks if the file is valid. The result is then saved in an external variable (in my case a ref) so it is stored through the whole dragging process. When the drag ends ("item" is null), the variable is cleared. |
* WIP Add a debugging case for hovering files Temporarily, remove before merging. * Load dataTransfer in every dragenter and dragover Chrome (and apparently Safari too) will empty the `DataTransferItemList` after a given drag event is done so we need to point `NativeDragSource` to a new list on each dragenter/dragover event. See https://bugs.chromium.org/p/chromium/issues/detail?id=137231 Fixes #584 * chore: cut semver * fix: run prettier Co-authored-by: Chris Trevino <chtrevin@microsoft.com>
* WIP Add a debugging case for hovering files Temporarily, remove before merging. * Load dataTransfer in every dragenter and dragover Chrome (and apparently Safari too) will empty the `DataTransferItemList` after a given drag event is done so we need to point `NativeDragSource` to a new list on each dragenter/dragover event. See https://bugs.chromium.org/p/chromium/issues/detail?id=137231 Fixes #584 * chore: cut semver * fix: run prettier Co-authored-by: Chris Trevino <chtrevin@microsoft.com>
I want to restrict allowed files extension and need to know what files user is dragging, but console throws warning:
NativeDragSources.js:61 Browser doesn't allow reading "files" until the drop event.
So what is the best way of checking file extension or dragging files count before dropping it on drop target?The text was updated successfully, but these errors were encountered: