問題描述
我有一個簡單的 Web 表單,可以通過 .NET C# 發送和發送電子郵件.此外,還有一個 ASP UPLOAD ctrl,我從中創建一個內存流并將該流附加到郵件消息...即無需將文件保存到文件系統.
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.
問題:當 MAC 用戶在他們上傳的文件中不包含擴展名時(我正在測試 PostedFile.ContentType 以確定文件和/或適當的擴展名、允許的文件 RTF、TXT、PDF、DOC、DOCX),它看起來像 ContentType以應用程序/八位字節"的形式返回……這對我來說很有意義,沒有 PC 要求的擴展,但是;仍然不清楚天氣讀取 ContentType 或擴展名是在我附加之前確定它是什么類型的文件的更好方法.[無擴展的 MAC 文件顯然無法通過任何擴展測試]
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]
基本上,我從上傳 ctrl 讀取文件,確定它的 ContentType 是什么,只剝離 PostedFile.Filename,然后在我使用 switch 語句中的 ContentType 字符串創建附件時重新組裝,以確定并添加正確的擴展名附件(文件名+分機).我這樣做是為了確保不使用擴展的 MAC 用戶仍然可以在不創建 .ZIP 或其他包的情況下上傳.
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.
問題 1:關于處理這個問題,我的邏輯是否合理,最佳實踐?問題 2:當不在 MAC 上使用擴展名時,所有 MAC 文件都會返回為application/octect"嗎?如果是這樣,有人如何確定它到底是什么類型的文件?(注意:在 MAC 平臺上不使用擴展時,PDF 工作正常).
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.
推薦答案
不要使用文件擴展名來計算 MIME 類型,而是使用Winista"進行二進制分析,請參閱我的方法here,如果 winista 則求助于 URLMon無法解決最后一個答案http://social.msdn.microsoft.com/Forums/en-US/Vsexpressvcs/thread/d79e76e3-b8c9-4fce-a97d-94ded18ea4dd
假設有人用 jpg 擴展名重命名了一個 exe……你仍然可以確定真實"的文件格式.它不會檢測 swf 或 flv,但幾乎可以檢測所有其他眾所周知的格式 + 你可以獲取一個十六進制編輯器并添加更多它可以檢測到的文件.
下載 Winista:http://www.netomatix.com/Products/DocumentManagement/MimeDetector.aspx
Download Winista: http://www.netomatix.com/Products/DocumentManagement/MimeDetector.aspx
這個問題給了我一個好主意,因為您希望附加一個可以在 MAC 和 PC 上安全運行的文件擴展名我/您可以編輯 mime-types.xml 文件以包含可以打開的安全文件擴展名MAC 和 PC 上的特定文件類型.
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.
這篇關于確定 MAC 上傳流文件的 MIME 類型的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!