1

I have a simple web form that sends and email out via .NET C#. Additionally there is an ASP UPLOAD ctrl from which I create a memory stream and attach the stream to the Mail Message...ie no need to save file to file system.

ISSUE: When MAC users dont inlcude extensions on their uploaded files (I am testing for PostedFile.ContentType to determine the file and/or appropriate extension, allowable files RTF, TXT, PDF, DOC, DOCX) it seems like the ContentType comes back as "application/octet"...this makes some sense to me w/o an extension as required by PCs but; still unclear on weather reading the ContentType or extension is a better way to determine what type of file it is before I attach. [Extensionless MAC file will obviously fail any extension test]

Basically, I read the file from the upload ctrl, determine what its ContentType is, strip just the PostedFile.Filename and then reassemble as I create the attachment using the ContentType string in a switch statement to determine and add the correct extension to the attached file (filename + ext). I did this in order to ensure MAC users that dont use extensions can still upload w/o creating a .ZIP or other package.

Question 1: Is my logic sound with regards to approaching this issue, best practices? Question 2: Will all MAC files return as "application/octect" when not using an extension on MAC, and if so, how can someone determine what type of file it really is? (Note: PDF worked fine when not using extensions on MAC platform).

Thanks in advance. If code sample is necessary please advise.

user699318
  • 13
  • 4
  • ContentType is set by the client, who uploads the file, and there is no guanantee that it's the correct type for the uploaded file. Neither is the file extension for that matter. Why do you need to know what type the file is? Do you need to run some workflow depending on the type? Or do you just save it? – Jesper Fyhr Knudsen Jan 04 '12 at 22:20
  • unless you have some way of knowing the difference of a .DOC vs a .DOCX vs a .TXT if they forget the extension.. humm I would probably change the logic to make sure before they upload a file that a filter is added to the upload code if there is not file ext or basically have an filer of allowable types.. otherwise don't allow the upload... and pdf probably worked fine because the content type matched the acatual file type.. but how many other cases will this work or not work for..?? – MethodMan Jan 04 '12 at 22:25
  • Arkain: Basically, my goal is to save the file into an email as an attachment. Secondly, the emails and/or attachment generated by the application will be mailed to both PC users and MAC platform users....so with that in mind I tried to find a common denominator (safety net), which to me was always attach the file using an .EXT which in the case of MAC users that dont include extensions makes this excersize difficult at best since what you're stating is that ContentType is really a 'crap shoot' in this context, or unless I compare the byte order to some external libraries etc. – user699318 Jan 04 '12 at 22:47
  • DJ: Basically my thinking was this...If PostedFile.ContentType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document" then when attaching the file to email do this... Filename + ".DOCX" this was my assumption, that if ConentType was a certain string then I can add an extension to the file to allow a clients machine find the appropriate applicatoin to open the file (if available) – user699318 Jan 04 '12 at 22:50
  • DJ: So how would you test for your allowable types w/o using ContentType or .EXT from file name? I do have allowable contenttypes: allowedContentTypes = { "application/pdf", "text/plain", "application/msword", "text/rtf", "application/vnd.openxmlformats-officedocument.wordprocessingml.document" }; – user699318 Jan 04 '12 at 22:58

1 Answers1

0

Dont use File Extensions to work out MIME Types, instead use "Winista" for binary analysis, see my method here that resorts back to URLMon if winista cant work it out last answer http://social.msdn.microsoft.com/Forums/en-US/Vsexpressvcs/thread/d79e76e3-b8c9-4fce-a97d-94ded18ea4dd

Say someone renames a exe with a jpg extension... you can still determine the "real" file format. It doesn't detect swf's or flv's but does pretty much every other well known format + you can get a hex editor and add more files it can detect.

Edit: Download Winista: http://www.netomatix.com/Products/DocumentManagement/MimeDetector.aspx

This question has given me a great idea, since you wish to append a File Extension that will safely work on MACs and PCs I/you could edit the mime-types.xml file to include safe file extensions that will work to open specific file types on both MAC and PCs.

Jeremy Thompson
  • 61,933
  • 36
  • 195
  • 321
  • Tried the Winista library, worked 'better' at identifying the bytes but still (unfortunatley) returned .DOCX as a .ZIP...which I get, but still doesnt get to the "BEST" implementation but certainly "better" – user699318 Jan 09 '12 at 22:45