599

I've seen application/csv used and also text/csv.

Is there a difference? Does it matter which as long as the request matches something that's available? Are they interchangeable?

ivanleoncz
  • 9,070
  • 7
  • 57
  • 49
Steve Dunn
  • 21,044
  • 11
  • 62
  • 87
  • 4
    For those who are in doubt regarding Media Types, I leave here this document from IANA which provides a list of registered Media Types: https://www.iana.org/assignments/media-types/media-types.xhtml – ivanleoncz Sep 02 '20 at 17:02

5 Answers5

780

RFC 7111

There is an RFC which covers it and says to use text/csv.

This RFC updates RFC 4180.


Excel

Recently I discovered an explicit mimetype for Excel application/vnd.ms-excel. It was registered with IANA in '96. Note the concerns raised about being at the mercy of the sender and having your machine violated.

Media Type: application/vnd.ms-excel

Name Microsoft Excel (tm)

Required parameters: None

Optional parameters: name

Encoding considerations: base64 preferred

Security considerations: As with most application types this data is intended for interpretation by a program that understands the data on the recipient's system. Recipients need to understand that they are at the "mercy" of the sender, when receiving this type of data, since data will be executed on their system, and the security of their machines can be violated.

OID { org-id ms-files(4) ms-excel (3) }

Object type spreadsheet

Comments This Media Type/OID is used to identify Microsoft Excel generically (i.e., independent of version, subtype, or platform format).

I wasn't aware that vendor extensions were allowed. Check out this answer to find out more - thanks starbeamrainbowlabs for the reference.

Community
  • 1
  • 1
Chris McCauley
  • 25,824
  • 8
  • 48
  • 65
  • 56
    CSV is a text file format, why would Excel have anything to do with it, or am I missing something? – JimmyPena Aug 08 '12 at 20:08
  • 2
    If you have installed excel on Mac OS the .csv filetype is bound to excel unless you change it. – Rutger Karlsson Jul 11 '13 at 09:23
  • 33
    That a program opens a file type doesn't have anything to do with the associated MIME type of that file type. – Pablo Pazos Jul 27 '15 at 05:08
  • 1
    @Pablo, nobody said it did. I said there's an entry for Excel in the mimetypes database - odd but ok I think. Rutger separately said that .csv opens in Excel on Mac (if installed). – Chris McCauley Jul 28 '15 at 10:51
  • 11
    @Pablo I have Excel installed on a Windows machine, and creating a CSV in Notepad++ will assign a MIME type of application/vnd.ms-excel. Try it for yourself and check your MIME type at http://mime.ritey.com/ – pjd Aug 05 '15 at 15:08
  • 28
    Notepad++ doesnt "assign" any mime type, the service you use is assigning it. – Pablo Pazos Aug 06 '15 at 16:25
  • 3
    @pjd - Pablo's correct, Notepad doesn't assign the MIME type - actually the service doesn't assign it either though as it is done by the "Network Working Group" mentioned in the RFC – Chris McCauley Aug 31 '15 at 15:32
  • @Pablo and Chris McCauley: Good to know. Interesting that I am seeing different MIME types on different computers, though. A Linux box vs a Windows box with Excel installed. – pjd Aug 31 '15 at 15:40
  • 1
    It's really down to whatever is in /etc/mime.types on Linux - not sure how Windows manages it. – Chris McCauley Sep 04 '15 at 10:01
  • 6
    Windows manages its list of mimetypes in the registry at `HKLM\Software\Classes` - the `Content Type` defined for the .csv entry when Excel is configured as the default application to handle them is `application/vnd.ms-excel` and so this is the value passed in. – Zhaph - Ben Duguid Sep 24 '15 at 14:15
  • 1
    @Nuno - that's why I didn't link the "text/csv" mime type to Excel in my answer but pointed out that there is a specific mime type to use if you really are sending an Excel worksheet. – Chris McCauley Jul 04 '16 at 09:56
  • 1
    @ChrisMcCauley The CSV is a format specification, it does not tell which application will open it. The mimetype should be "text/CSV" and the user's system should open the file with this mimetype with the appropriate application (eg: excel, notepad++, etc). It is a workaround or a decision you make to force it to open the file using excel. Kind regards. – Nuno Aniceto Sep 22 '16 at 20:47
  • 1
    @NunoAniceto, I'm not saying that it does. I'm answering the OPs question and observing that there's an additional MIME type which will be of interest to Excel users. – Chris McCauley Sep 23 '16 at 13:45
  • 3
    For those curious as to what the `vnd` stands for in this answer, check out [this answer](https://stackoverflow.com/questions/5351093/what-is-the-meaning-of-vnd-in-mime-types/5351162) – starbeamrainbowlabs Mar 20 '17 at 11:51
  • 1
    the link to the mimetype database shows no info pn text/csv nor application/csv... – Henning Jun 09 '17 at 14:56
  • 1
    @Henning, Thanks I've updated the post to point to a better source and quoted the relevant parts. – Chris McCauley Jun 10 '17 at 15:12
  • 8
    It's not about being butt-hurt, but about 80 percent of the answer being, in fact, off-topic. – Stijn de Witt Sep 15 '17 at 14:56
  • 1
    @StijndeWitt The first line directly answers the question asked. The rest is as others have said, is interesting. That's not surprising given how many folks use Excel to open CSV files. – Chris McCauley Sep 24 '19 at 10:13
  • 2
    As someone who found this answer from a Google search because of a random UI validation error, this answer was hugely relevant. – Woody1193 Feb 04 '22 at 01:58
  • 1
    interestingly I had a script setup for 'application/vnd.ms-excel' (for 2-3 years) - but today I had to add text/csv as an alternative because the csv (edited in Excel as normal - the colleague says) did not have the mime-type of 'application/vnd.ms-excel' - has something changed recently? – UrbanwarfareStudios Apr 05 '22 at 08:29
  • @UrbanwarfareStudios Not sure if it helps but there's an earlier comment that says "Windows manages its list of mimetypes in the registry at HKLM\Software\Classes - the Content Type defined for the .csv entry when Excel is configured as the default application to handle them is application/vnd.ms-excel and so this is the value passed in. – Zhaph - Ben Duguid Sep 24, 2015 at 14:15" – Chris McCauley Apr 06 '22 at 12:24
121

You should use "text/csv" according to RFC 4180.

Community
  • 1
  • 1
Smokefoot
  • 1,695
  • 2
  • 10
  • 13
  • 1
    Does "text/csv" accept ".txt" files as well? I am working on applying validation on server side to just accept ".csv" file types. But the ".txt" files also get accepted.Please confirm. – Sukanya Pai Mar 25 '19 at 06:08
  • 1
    sorry but havn't seen your question so far. RFC 4180 makes sure that the extension should be .csv. Nevertheless a you might know, it's not a strict requirement. You can break the implementation of the RFC 4180, but personally I would not do that, because it is disturbing the readability of the application. – Smokefoot May 20 '19 at 11:19
  • Files edited with Excel will show up as `application/vnd.ms-excel` – Kevin Van Ryckegem Oct 31 '21 at 10:26
59

Strange behavior with MS Excel: If i export to "text based, comma-separated format (csv)" this is the mime-type I get after uploading on my webserver:

[name] => data.csv
[type] => application/vnd.ms-excel

So Microsoft seems to be doing own things again, regardless of existing standards: https://en.wikipedia.org/wiki/Comma-separated_values

aefxx
  • 24,835
  • 6
  • 45
  • 55
Robert
  • 759
  • 5
  • 7
  • 5
    You cant really trust the mime-type sent by web clients, its just a guide generally. – Chris Seufert Jul 20 '15 at 04:16
  • 7
    This is due to the configuration of the web-server which maps file extension to mime type. Microsofts IIS should use the servers registry for this which is populated by the Excel installation if it is installed on the web server. – Andrew Russell Dec 09 '15 at 01:50
  • 5
    Well...Considering that the "existing standard" decided to ignore what was already in use when they decided to define CSV in RFC 4180 which was written in October of 2005, it would be silly to blame Microsoft for not jumping into the future to see what the standards body decides and then use that back when they first wrote excel back in 1985. What version of excel are you installing? – Robert McKee Jun 10 '16 at 15:30
  • 1
    On top of everything else, excel will mangle the field values in a CSV file. – Ronnie Overby Jul 26 '16 at 20:52
  • maybe, it depends what "after uploading" actually means. – Jasen Apr 23 '21 at 04:46
57

My users are allowed to upload CSV files and text/csv and application/csv did not appear by now. These are the ones identified through finfo():

text/plain
text/x-csv

And these are the ones transmitted through the browser:

text/plain
application/vnd.ms-excel
text/x-csv

The following types did not appear, but could:

application/csv
application/x-csv
text/csv
text/comma-separated-values
text/x-comma-separated-values
text/tab-separated-values
Community
  • 1
  • 1
mgutt
  • 5,867
  • 2
  • 50
  • 77
  • 3
    Browsers guess what to send as the MIME type of a file during upload. You can reject out of hand ones you want nothing to do with, but checking the actual content of the file with care is the only way to be sure what's been uploaded. – Tetsujin no Oni Feb 24 '17 at 07:13
  • text/tab-separated-values is technically not CSV file; or do you mean you can also get it even for comma separated data? – Arthur Oct 28 '18 at 11:09
  • 1
    @Arthur Feel free to use an additional TSV file category. I ignore the other terms because many programs create a file with the .csv extension no matter which delimiter is used and PHP - as the most used server-side language- uses the term CSV in it's functions only, too. Call it "character separated files" and you're fine ;) – mgutt Oct 29 '18 at 13:41
15

For anyone struggling with Google API mimeType for *.csv files, I have found the list of MIME types for google api docs files

Google Doc Format Conversion Format Corresponding MIME type
Documents HTML text/html
HTML (zipped) application/zip
Plain text text/plain
Rich text application/rtf
Open Office doc application/vnd.oasis.opendocument.text
PDF application/pdf
MS Word document application/vnd.openxmlformats-officedocument.wordprocessingml.document
EPUB application/epub+zip
Spreadsheets MS Excel application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
Open Office sheet application/x-vnd.oasis.opendocument.spreadsheet
PDF application/pdf
CSV (first sheet only) text/csv
TSV (first sheet only) text/tab-separated-values
HTML (zipped) application/zip
Drawings JPEG image/jpeg
PNG image/png
SVG image/svg+xml
PDF application/pdf
Presentations MS PowerPoint application/vnd.openxmlformats-officedocument.presentationml.presentation
Open Office presentation application/vnd.oasis.opendocument.presentation
PDF application/pdf
Plain text text/plain
Apps Scripts JSON application/vnd.google-apps.script+json

Source here: https://developers.google.com/drive/v3/web/manage-downloads#downloading_google_documents the table under: "Google Doc formats and supported export MIME types map to each other as follows"

There is also another list

MIME Type Description
application/vnd.google-apps.audio
application/vnd.google-apps.document Google Docs
application/vnd.google-apps.drawing Google Drawing
application/vnd.google-apps.file Google Drive file
application/vnd.google-apps.folder Google Drive folder
application/vnd.google-apps.form Google Forms
application/vnd.google-apps.fusiontable Google Fusion Tables
application/vnd.google-apps.map Google My Maps
application/vnd.google-apps.photo
application/vnd.google-apps.presentation Google Slides
application/vnd.google-apps.script Google Apps Scripts
application/vnd.google-apps.site Google Sites
application/vnd.google-apps.spreadsheet Google Sheets
application/vnd.google-apps.unknown
application/vnd.google-apps.video
application/vnd.google-apps.drive-sdk 3rd party shortcut

Source here: https://developers.google.com/drive/v3/web/mime-types

But the first one was more helpful for my use case.

Cameron Tacklind
  • 5,764
  • 1
  • 36
  • 45
Luckylooke
  • 4,061
  • 4
  • 36
  • 49