Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DropZone: How to Accept .csv Files?

I've read quite a few answers on SO about this, and most advise using the acceptedFiles property to specify the accepted mime types.

However, the DropZone docs say:

Mime type determination is not reliable across platforms. CSV files, for example, are reported as text/plain under macOS but as application/vnd.ms-excel under Windows. In some cases there might not be a mime type set at all.

I'm trying to upload a .csv file, and (using material-ui-dropzone), so far I've tried:

  <Dropzone
    acceptedFiles={['.csv', 'text/*']}
    showPreviews={true}
    showFileNamesInPreview={true}
  />

  <Dropzone
    acceptedFiles={'.csv', 'text/*'}
    showPreviews={true}
    showFileNamesInPreview={true}
  />

  <Dropzone
    acceptedFiles={'.csv', 'text/csv'}
    showPreviews={true}
    showFileNamesInPreview={true}
  />

...etc., but so far none are working:

  • The open file dialog shows .csv files as grayed out
  • Dragging-and-dropping a .csv file to DropZone gets a "File SeriesNotes.csv was rejected. File type not supported." message

What is the correct way to solve this for material-ui-dropzone (or for any version of DropZone)?

like image 905
VikR Avatar asked Feb 21 '20 00:02

VikR


People also ask

How do you import a CSV file?

On the Data tab, in the Get & Transform Data group, click From Text/CSV. In the Import Data dialog box, locate and double-click the text file that you want to import, and click Import. In the preview dialog box, you have several options: Select Load if you want to load the data directly to a new worksheet.

What is dropzone file upload?

js is one of the most popular drag and drop JavaScript libraries. It is free, fully open source, and makes it easy for you to handle dropped files on your website. It's meant to look good by default, and is highly customizable. Documentation Download.

How to make the dropzone accept specific file types?

By providing accept prop you can make the dropzone accept specific file types and reject the others. The value must be an object with a common MIME type as keys and an array of file extensions as values (similar to showOpenFilePicker 's types accept option).

Does dropzone work with JavaScript?

Whether your users use an outdated browser, or have JavaScript enabled, Dropzone got you covered. One main goal when creating Dropzone was to have file previews that are not only practical, but also look good. That's why the default design of Dropzone looks great without you needing to do anything. Need a server?

Why can't dropzone read the file extensions during the drag operation?

Because of HTML5 File API differences across different browsers during the drag, Dropzone might not be able to detect whether the files are accepted or rejected in Safari nor IE. Furthermore, at this moment it's not possible to read file names (and thus, file extensions) during the drag operation.

Where can I find the source code for dropzone?

You can get all the source code on GitHub, as well as installation instructions. If you encounter an issue with this library, this is the place to create an issue. All the documentation about Dropzone, and the multiple ways to configure and customise it, can be found on GitBook. If you need help, there are GitHub Discussions and Stackoverflow.


1 Answers

After lots of trial and error, this worked for me.

acceptedFiles={[".csv, text/csv, application/vnd.ms-excel, application/csv, text/x-csv, application/x-csv, text/comma-separated-values, text/x-comma-separated-values"]}

The main one on windows was the .csv the rest are just in case.

like image 131
user1767752 Avatar answered Oct 24 '22 09:10

user1767752