更改扩展后如何查找真实文件mime类型(在c#中)(特别是对于Excel 2007)? [英] How to find real file mime type (in c#) after changing extention (especially for Excel 2007)?

查看:75
本文介绍了更改扩展后如何查找真实文件mime类型(在c#中)(特别是对于Excel 2007)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些文件(它们应该是Excel 2003/2007 / ...)我想检查它们是否真的是没有危险脚本的Excel文件等等,我怎么能在C#中做到?



请参阅此plz: http://forum.sources.ru/mime。 php [ ^ ]



在这个页面,即使你改变文件扩展名它也可以找到真正的mime类型

解决方案

你不能在客户端上做到这一点方面,因为您不能假设客户端系统具有任何所需的软件,即使该客户端发送了Excel文件。在服务器端,它不会是一个大问题。



严格来说,你不能确切地知道文件是什么,直到你试图解析它。请参阅我对解决方案1的评论。您可以解压缩.xlxs并查看其中的内容。了解应该在那里做什么并做一些检查。或者您可以使用Microsoft Open XML SDK并尝试通过SDK直接读取文件。



请参阅我过去的答案:

< a href =http://www.codeproject.com/Answers/575774/Extractplusfileplusrarpluswithplusvb-net#answer1>使用vb.net提取文件rar [ ^ ],

如何在MS Visual Studio 2010中添加Microsoft Reference excel 15.0对象库 [ ^ ]。



- SA


文件的类型由它的幻数决定,它是放在文件开头的一系列字节。要计算出它是Excel,jpg等你需要知道文件格式的幻数,然后检查文件开头的字节数,看它们是否匹配



http://www.garykessler.net/library/file_sigs.html [ ^ ]



您可以通过将文件转换为byte []来读取字节。如果你google读取文件幻数c#,我相信你会找到一些例子。


看看



Quote:

一种方法是检查文件中的某些签名或幻数。这个页面有一个方便的已知文件签名列表,似乎是最新的:





文件签名表 [ ^ ]


hi , I have some files ( they should be Excel 2003/2007/...) and I wanted to check if they are really Excel files without dangerous scripts and so on , how can I do it in C# ?

see this plz : http://forum.sources.ru/mime.php[^]

in this page even if u change a file extention it can find the real mime type

解决方案

You cannot do it on the client side, because you cannot assume the client-side system has any of required software, even if that client sent an Excel file. And on server side it won't be a big problem.

Strictly speaking, you cannot know what is the file exactly, until you try to parse it all. Please see my comment to Solution 1. You can unzip .xlxs and see what inside. Learn what it should be there and do some check up. Or you can use Microsoft Open XML SDK and try to read the file directly via the SDK.

Please see my past answers:
Extract file rar with vb.net[^],
How to add microsoft excel 15.0 object library from Add Reference in MS Visual Studio 2010[^].

—SA


The type of a file is determined by it's "magic number" which is a series of bytes that it places at the start of the file. To work out if it is Excel, jpg etc you need to know that file format's magic number, then check the bytes at the start of the file you have and see if they match

http://www.garykessler.net/library/file_sigs.html[^]

You can read the bytes by turning your file into byte[]. If you google "read file magic number c#" I'm sure you'll find some examples.


Take a look:

Quote:

One way would be to check for certain signatures or magic numbers in the files. This page has a handy list of known file signatures and seems quite up to date:



FILE SIGNATURES TABLE[^]


这篇关于更改扩展后如何查找真实文件mime类型(在c#中)(特别是对于Excel 2007)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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