文件上传有效期 [英] File upload validatios

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

问题描述



我在使用asp.net开发的应用程序中需要验证文件上传功能。

验证器应该只允许doc,pdf文件。即使用户试图通过交换扩展名来输入恶意文件

,验证者也不应该允许。

请让我知道如何在C#中检测重命名的扩展名



提前致谢

Hi,
I need a validator for file upload functionality in my application developed using asp.net.
the validator should allow only doc,pdf files. Even if the user tries to enter malicious files
by exchanging the extension,the validator should not allow.
Please let me know how to detect renamed extensions in C#

Thanks in advance

推荐答案

这是一个答案...



this is an answer ...

if (FileUpload1.HasFile)
        {
            if (FileUpload1.FileName.EndsWith("pdf") || FileUpload1.FileName.EndsWith("docx"))
            {
               // save file Cods ... 
            }
            else
            {
                //do nothing !!
                Response.Write("Error!!!");
            }
        }


尝试这样的事情:



Try something like this:

string filename = "C:\\" + FileUpload1.FileName.ToString();

if (filename.EndsWith(".doc") || filename.EndsWith(".pdf"))
{
    FileUpload1.SaveAs(filename);
}
else
{
   //don't do stuff
}





这很简单,但应该适合你需要的东西。当然,您将为保存的文件选择适当的路径



It is simple, but should work for what you need. Of course you will chose the appropriate path for your saved file


如果要检测文件的实际类型而不是扩展名,那么您需要查看检索MIME类型,例如:



http://stackoverflow.com/questions/11547654/determine-the-file-type-using-c-sharp [ ^ ]
If you want to detect the actual type of the file rather than the extension, then you will need to look at retrieving the MIME type, something like:

http://stackoverflow.com/questions/11547654/determine-the-file-type-using-c-sharp[^]


这篇关于文件上传有效期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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