Determine what is being dragged from dragenter & dragover events

前端 未结 9 1942
梦毁少年i
梦毁少年i 2020-12-04 17:35

I\'m trying to use the HTML5 draggable API (though I realize it has its problems). So far, the only showstopper I\'ve encountered is that I can\'t figure out a way to determ

9条回答
  •  [愿得一人]
    2020-12-04 17:49

    To check if it is a file use:

    e.originalEvent.dataTransfer.items[0].kind
    

    To check the type use:

    e.originalEvent.dataTransfer.items[0].type
    

    i.e. I want to allow only one single file jpg, png, gif, bmp

    var items = e.originalEvent.dataTransfer.items;
    var allowedTypes = ["image/jpg", "image/png", "image/gif", "image/bmp"];
    if (items.length > 1 || items["0"].kind != "file" || items["0"].type == "" || allowedTypes.indexOf(items["0"].type) == -1) {
        //Type not allowed
    }
    

    Reference: https://developer.mozilla.org/it/docs/Web/API/DataTransferItem

提交回复
热议问题