Create CSV file with text/csv MIME type

I'm attempting to create a CSV file, but unfortunately, the file has a MIME type of text/plain. How can I change the MIME type to text/csv?

The MIME type of the result file is text/plain regardless of the extension that I give the file.

I've searched high and low for a solution, but haven't found one yet.

Any help is much appreciated. Thank you!

Answered by endecotp in 721262022

The file command inspects the content of the file to determine the type.

I don't know exactly what it looks for to identify a CSV file, but could it be that something like line-break characters are causing the difference? CSV (as defined by RFC4180) is supposed to use CRLF line breaks. I could imagine that whatever you have used to modify it has leniently accepted e.g. LF line breaks and saved it with the required CRLF. (Less likely with a "text editor" (be specific!!) and more likely with a spreadsheet.)

If you post the output of e.g. od -a | head for both files, maybe someone will spot the important difference.

Be aware that what the file command things may not be very important in general on MacOS; what Splotlight thinks makes more difference to e.g. Finder.

P.S. I suggest not posting comments on this forum - post replies. Comments don't seem to trigger emails to people who are watching the thread.

Mac or iOS? Where are you observing the MIME type?

Like endecotp, I’m curious how you are observing the MIME type. You wrote:

If I open and re-save the file using a text editor, the MIME type changes.

What UI is displaying the MIME type? Or is this an API you’re calling to get the MIME type?

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Accepted Answer

The file command inspects the content of the file to determine the type.

I don't know exactly what it looks for to identify a CSV file, but could it be that something like line-break characters are causing the difference? CSV (as defined by RFC4180) is supposed to use CRLF line breaks. I could imagine that whatever you have used to modify it has leniently accepted e.g. LF line breaks and saved it with the required CRLF. (Less likely with a "text editor" (be specific!!) and more likely with a spreadsheet.)

If you post the output of e.g. od -a | head for both files, maybe someone will spot the important difference.

Be aware that what the file command things may not be very important in general on MacOS; what Splotlight thinks makes more difference to e.g. Finder.

P.S. I suggest not posting comments on this forum - post replies. Comments don't seem to trigger emails to people who are watching the thread.

After using the od -a command, I noticed that the line-break characters are different between the bad and good CSV. I changed the line-breaks from \n to \r\n. The file command shows the MIME type as text/csv now! Thank you @endecotp!

Create CSV file with text/csv MIME type
 
 
Q