确定 MAC 上传流文件的 MIME 类型 [英] Determining MIME Type of MAC upload stream file

查看:62
本文介绍了确定 MAC 上传流文件的 MIME 类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的 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 类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆