如何确定 .NET 中文件的内容类型?
我得到了一个文件名,我必须能够从磁盘读取它并通过网络发送其内容。 我需要能够确定文件是文本还是二进制文件,以便我知道是否使用 StreamReader 还是 BinaryReader。 我需要知道内容类型的另一个原因是,如果它是二进制的,我必须在通过网络发送数据之前对数据进行 MIME 编码。 我还希望能够告诉消费者内容类型是什么(如果是文本,则包括编码)。
谢谢!
I'm given a filename and I have to be able to read it from disk and send its contents over a network. I need to be able to determine whether the file is text or binary so I know whether to use a StreamReader or BinaryReader. Another reason why I need to know the content type is because if it is binary, I have to MIME encode the data before sending it over the wire. I'd also like to be able to tell the consumer what the content type is (including the encoding if it is text).
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
文件扩展名提供了有关文件内容类型的最佳提示。
它并不完美,但我使用以下方法取得了一些成功:
The filename extension provides your best hint at the content type of a file.
It's not perfect, but I've used the following with some success:
您要么需要提前知道文件的类型,要么被告知文件是什么类型。 如果您所做的只是通过网络发送文件,为什么不直接使用 Stream.Write 和 Stream.Read 方法呢? 让服务的使用者确定文件类型。 您不需要使用 *Reader 类,因为您不需要解释服务器上的数据。
You either need to know the type of file ahead of time, or be told what type the file is. And why don't you just use the Stream.Write and Stream.Read methods if all you're doing is sending the file over a network? Let the consumer of the service determine the file type. You don't need to use the *Reader classes since you aren't interpreting the data on your server.
您可以尝试这个免费的 mime 检测器库。
http://www.netomatix.com/Products/DocumentManagement/MimeDetector.aspx
You can try this free mime detector library out.
http://www.netomatix.com/Products/DocumentManagement/MimeDetector.aspx