react-dropzone: isDragReject true for accepted file extension

I have seen this work appropriately when setting the MIME type but has not been working when setting the file extension. I want to set a more specific extension, but for the sake of the bug I have been seeing .png being rejected when choosing to accept .png files. The hidden input is working as expected, but the hover and subsequent event is not working as expected.

React dev tools showing current accept: screen shot 2016-05-10 at 4 45 54 pm

Screenshot of rejection: drag-reject

I apologize if I’ve missed something. I’m looking back at your attr-accept project to trace it back.

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Reactions: 2
  • Comments: 24

Most upvoted comments

This however doesn’t fix the problem for files with unspecified MIME types. Any hints on how to handle that case?

image

Any chance this issue can be reopened, so no new ones need be created?

@lruckman thx, for those coming here from Google try this accept=".csv,text/csv"

Shouldn’t a combination of mime’s and extensions in the accept list address this?

So I’m thinking this has to do with how isDragReject is being set:

https://github.com/react-dropzone/react-dropzone/blob/ccb859f1eaa43683952bc4f3d4b6d3283a8582a8/src/index.js#L310-L311

as per the documentation, we’re unable to read the file name mid-drag, only the mime-type. So in cases where we have an accept that isn’t a mime, filesCount > 0 will be true, and isDragAccept will be false because of how attr-accept validates the mimes:

    return acceptedFilesArray.some(type => {
      const validType = type.trim()
      if (validType.charAt(0) === '.') {
        return fileName.toLowerCase().endsWith(validType.toLowerCase())
      } else if (/\/\*$/.test(validType)) {
        // This is something like a image/* mime type
        return baseMimeType === validType.replace(/\/.*$/, '')
      }
      return mimeType === validType
    })

https://github.com/okonet/attr-accept/blob/master/src/index.js#L24-L33

My initial thoughts are to check what kind of accept prop we have (mime or extension) and include that in the setting of isDragReject, something like:

const acceptIsMime = /\/\*$/.test(this.props.accept)

const isDragReject = acceptIsMime && filesCount > 0 && (!isDragActive || !isMultipleAllowed)

Maybe the test for what kind of accept we have should actually live in fileAccepted?

How do we feel about this solution?

/cc @rkmarks / @okonet

Thanks @ArtemBernatskyy : You are close to god 😃 It solved my csv issue.

I’ll have some time tomorrow to work on this. Though might have something w/ attr-accept npm module. Not sure. I’ll dig in