如何在C#中查找文件的扩展名?
在我的网络应用程序(asp.net,c#)中,我正在页面中上传视频文件,但我只想上传 flv 视频。如何限制上传其他扩展视频?
In my web application (asp.net,c#) I am uploading video file in a page but I want to upload only flv videos. How can I restrict when I upload other extension videos?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(14)
Path.GetExtension
Path.GetExtension
您可以简单地读取文件流,
前 5/6 索引将告诉您文件类型。对于 FLV,其值为 70, 76, 86, 1, 5。
如果
isAllowed
等于true
则其 FLV。或
读取文件的内容
前两个/三个字母将告诉您文件类型。
如果是 FLV,则为“FLV......”
You may simply read the stream of a file
First 5/6 indexes will tell you the file type. In case of FLV its 70, 76, 86, 1, 5.
if
isAllowed
equalstrue
then its FLV.OR
Read the content of a file
First two/three letters will tell you the file type.
In case of FLV its "FLV......"
在服务器上,您可以检查 MIME 类型,在此处或在 google 上查找 flv mime 类型。
您应该检查 mime 类型是否为
如果您在 C# 中使用 FileUpload 例如,您可以这样做
At the server you can check the MIME type, lookup flv mime type here or on google.
You should be checking that the mime type is
If you were using a FileUpload in C# for instance, you could do
此外,如果您有一个
FileInfo fi
,您可以简单地执行以下操作:它会保存文件的扩展名(注意:它将包含
.
,因此上面的结果可能是:.jpg
.txt
,依此类推......In addition, if you have a
FileInfo fi
, you can simply do:and it'll hold the extension of the file (note: it will include the
.
, so a result of the above could be:.jpg
.txt
, and so on....上述方法适用于 Firefox 和 IE:我可以查看所有类型的文件,如 zip、txt、xls、xlsx、doc、docx、jpg、png。
但是当我尝试从 Google Chrome 中查找文件扩展名时,我失败了。
The above method works fine with the Firefox and IE: I am able to view all types of files like zip,txt,xls,xlsx,doc,docx,jpg,png.
But when I try to find the extension of file from Google Chrome, I fail.
我不确定这是否是您想要的,但是:
或者:
I'm not sure if this is what you want but:
Or:
EndsWith()< /strong>
在 DotNetPerls 找到了一个替代解决方案,我更喜欢它,因为它不需要你指定一个路径。这是一个示例,我在自定义方法的帮助下填充了一个数组
EndsWith()
Found an alternate solution over at DotNetPerls that I liked better because it doesn't require you to specify a path. Here's an example where I populated an array with the help of a custom method
值得一提的是如何在获取扩展的同时删除扩展:
It is worth to mention how to remove the extension also in parallel with getting the extension:
您将无法限制用户在客户端上传的文件类型[*]。您只能在服务器端执行此操作。如果用户上传了错误的文件,您将只能在文件上传后才能识别。没有可靠且安全的方法来阻止用户上传他们想要的任何文件格式。
[*] 是的,您可以在开始上传之前执行各种巧妙的操作来检测文件扩展名,但不要依赖它。迟早有人会绕过它并上传他们喜欢的任何内容。
You will not be able to restrict the file type that the user uploads at the client side[*]. You'll only be able to do this at the server side. If a user uploads an incorrect file you will only be able to recognise that once the file is uploaded uploaded. There is no reliable and safe way to stop a user uploading whatever file format they want.
[*] yes, you can do all kinds of clever stuff to detect the file extension before starting the upload, but don't rely on it. Someone will get around it and upload whatever they like sooner or later.
您可以检查 .flv 签名。您可以在此处下载规范:
http://www.adobe.com/devnet/flv/
请参阅“FLV 标头”一章。
You can check .flv signature. You can download specification here:
http://www.adobe.com/devnet/flv/
See "The FLV header" chapter.
该解决方案还有助于解决多个扩展名(例如“Avishay.student.DB”)的情况
This solution also helps in cases of more than one extension like "Avishay.student.DB"
Path.GetExtension(file.FileName))
将为您提供文件名,如果有人需要测试和获取扩展名或名称,我还会共享测试代码。
形成一个名为 test.txt 的文本文件并在 xUnit 中检查其扩展名。
按预期返回 true,文件扩展名是 name。
希望这对某人有帮助:)。
Path.GetExtension(file.FileName))
will get you the file nameIm also sharing a test code if someone needs to test and ge the extention or name.
Forming a text file with name test.txt and checking its extention in xUnit.
Return true as the expected and the filename extention is name.
Hope this helps someone :).
我知道这是一个相当老的问题,但这里有一篇关于获取文件扩展名以及更多值的好文章:
在 C# 中获取文件扩展名
我希望有帮助:-)!
I know this is quite an old question but here's a nice article on getting the file extension as well as a few more values:
Get File Extension in C#
I Hope That Helps :-)!