验证由ASP.net中的内容上传的文件 [英] Validate file uploaded by its content in ASP.net

查看:83
本文介绍了验证由ASP.net中的内容上传的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我的要求是不同的......我已经通过我的模块中的扩展程序验证了文件上传,但我的客户端要求我不允许任何文件上传延期。 bcoz可能会将任何.exe / .apk或其他文件重命名为扩展名.jpg / .png并上传。检查要上传的文件的内容。检查要上传的文件的内容。



谷歌搜索相同并寻求帮助..提前致谢。

解决方案

你好,

参考:

在ASP.Net上载之前验证文件的MIME

谢谢


什么你正在寻找被称为神奇的数字。文件类型在它们的开头有一系列二进制数据,可以让您知道文件类型包含的内容,而不管其扩展名如何。您要做的是找到您感兴趣的类型的幻数(gif,jpg,png,如果允许的话,可能是pdf)并检查您上传的文件以这些数字开头。如果他们没有,那么删除\\'拒绝他们。



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



检查图像的一些代码,但你可以适应任何魔术数字;



http://stackoverflow.com/questions/670546/determine -if-file-is-an-image [ ^ ]



http://stackoverflow.com/questions/772388/c-sharp-how-can-i-test-a-file-is-a- jpeg [ ^ ]



如果您谷歌搜索c#check image magic number之类的内容,您会找到其他示例。

  if (fileupload.HasFile)
{
if (extension == 。exe || extension == 。apk
{
}
else
{
// 警报仅选择.exe或.apk文件
}
}


hi all,
My requirement is something different.. i have done file uploading with validating by its extension in my module, but my client asked me don't allow any file to upload just by extension. bcoz it may possible that any .exe/.apk or other files can be renamed to extension .jpg/.png and uploaded..Check the contents of file to be uploaded..Check the contents of file to be uploaded.

Googling for the same and looking for help.. Thanks in advance.

解决方案

Hello ,
Refer :
Validating MIME of a File Before Uploading in ASP.Net
Thanks


What you're looking for is called "magic numbers". File types have a sequence of binary data at the start of them that lets you know what the file type contains regardless of its extension. What you're going to have to do is find the magic numbers for the types you're interested in (gif, jpg, png, maybe pdf if you allow those) and check the files you have uploaded start with those numbers. If they don't then delete\reject them.

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

Some code to check for images, but you can adapt for any magic numbers;

http://stackoverflow.com/questions/670546/determine-if-file-is-an-image[^]

http://stackoverflow.com/questions/772388/c-sharp-how-can-i-test-a-file-is-a-jpeg[^]

If you google for things like "c# check image magic number" you'll find other examples.


if (fileupload.HasFile)
 {
     if (extension == ".exe"  || extension == ".apk" )
     {
     }
     else
     {
         // alert select only .exe or .apk files
     }
}


这篇关于验证由ASP.net中的内容上传的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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