ChatGPT解决这个技术问题 Extra ChatGPT

What MIME type should I use for CSV?

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?

For those who are in doubt regarding Media Types, I leave here this document from IANA which provides a list of registered Media Types: iana.org/assignments/media-types/media-types.xhtml

C
Community

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.


CSV is a text file format, why would Excel have anything to do with it, or am I missing something?
That a program opens a file type doesn't have anything to do with the associated MIME type of that file type.
@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 mime.ritey.com
Notepad++ doesnt "assign" any mime type, the service you use is assigning it.
It's not about being butt-hurt, but about 80 percent of the answer being, in fact, off-topic.
C
Community

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


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.
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.
Files edited with Excel will show up as application/vnd.ms-excel
a
aefxx

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


You cant really trust the mime-type sent by web clients, its just a guide generally.
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.
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?
On top of everything else, excel will mangle the field values in a CSV file.
maybe, it depends what "after uploading" actually means.
C
Community

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

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.
text/tab-separated-values is technically not CSV file; or do you mean you can also get it even for comma separated data?
@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 ;)
C
Cameron Tacklind

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.


Plus one because of the pretty formatted tables :)