How to Use the CSV Mime-Type

What MIME type should I use for CSV?

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.

How to use the CSV MIME-type?

You could try to force the browser to open a "Save As..." dialog by doing something like:

header('Content-type: text/csv');
header('Content-disposition: attachment;filename=MyVerySpecial.csv');
echo "cell 1, cell 2";

Which should work across most major browsers.

What mime type should I use for CSV ZIP files?

A zip file is a zip file, no matter what it contains.

It should be application/zip.

It is not a CSV file, if you tried to parse it as CSV it would fail. It is not text/csv.

Why am I getting mime-type of .csv file as application/octet-stream ?

In times like these, the official HTTP specification is always helpful. From RFC 2616 7.2.1 (my emphasis added):

Any HTTP/1.1 message containing an entity-body SHOULD include a Content-Type header field defining the media type of that body. If and only if the media type is not given by a Content-Type field, the recipient MAY attempt to guess the media type via inspection of its content and/or the name extension(s) of the URI used to identify the resource. If the media type remains unknown, the recipient SHOULD treat it as type "application/octet-stream".

The cause of your issue is that the server accepting the file upload does not itself know what type of file has been uploaded. Why? Because it relies on the the HTTP message which sent the file to specify a Content-Type header to determine the exact mime-type. The browser has likely not sent a Content-Type header and the server has assumed application/octet-stream as per the official HTTP specification excerpt above. It's also possible that the client uploading the file opted not to determine the mime type of the file it was uploading and sent the Content-Type: application/octet-stream header itself.

Now, when we consider this in conjunction with the PHP manual entry regarding POST file uploadsdocs, we see the following:

$_FILES['userfile']['type']

The mime type of the file, if the browser provided this information. An example would be "image/gif". This mime type is however not checked on the PHP side and therefore don't take its value for granted.

So as you can see, even if $_FILES['userfile']['type'] is specified, it only corresponds to the Content-Type header sent by the client. This information can easily be faked and should not be relied upon. If you need to be sure that the uploaded file is of a specific type, you'll have to verify that yourself.

Swift - Create CSV file with text/csv MIME type

Thank you everyone for your input!

I received some help from the Apple Developer Forums:
https://developers.apple.com/forums/thread/710757?page=1#721303022

Changing the line-break characters from \n to \r\n did the trick! The file command now shows the file's MIME type as text\csv.

What to set as mimetype for CSV files to open in spreadsheet applications

Try this:

Content-Type: text/csv; name="filename.csv"
Content-Disposition: attachment; filename="filename.csv"

In Windows browsers the MIME type is ignored after the file is downloaded and only file name extension is used to determine the corresponding application.

Also, making .CSV file open in Excel correctly is hard, because the default separator is taken from Regional settings on the machine on which the CSV is opened. Furthermore, the encoding is not determined from the file content or MIME type, so any non-ASCII characters are hard to get working.



Related Topics



Leave a reply



Submit