I'm creating a Dropzone component.
I would like to make my <div> red when the user tries to drag and drop an invalid file type.
Currently through dragstart event I can get the MimeType of the file, but I can't get the file name or extension.
The accept property of the Input type=file allows filtering files either by MimeType (example: image/png) or by extension (example: .png). I wish my Dropzone could filter both by MimeType and by extension, just like Input type=file.
So, keep in mind that the extension is not actually something you can or need to validate on. The raw file data is simply the bytes whereas the extension is used by the operating system to try and pick the correct application to present that set of bytes.
Also, the
acceptvalue for theinputelement doesn't actually limit the file types which can be entered into the field. HTML attribute: accept - HTML: HyperText Markup Language | MDNWhat I'm reading is that you're looking for validating that the document they are trying to drag into your dropzone is the correct file type which can only be done based on the
mime-typeof the document itself which is possible and should be performed by both your front-end and backend applications. Common MIME types - HTTP | MDNIn the
DragEventunderdataTransfer.items, you can convert that to an array usingArray.fromthen iterate over each item, you have exposed theitem.kindanditem.type.item.kindis eitherfileortextitem.typewhen thekindisfilethis will give you a hint to themime-typewhich the user is attempting to drag into your dropzone.You would need to write a parser that would be able to take your
accept-mime-type and validate this value.I've mocked up something which could help accomplish this for you.
Keep in mind:
onDropusing a similar method and alsoonChangeif using an<input type="file" />for manual uploads