首页 新闻 会员 周边

判断上传文件类型时 出错“未能找到文件”怎么办?

0
悬赏园豆:10 [已关闭问题]

 用FileUpload1上传文件时,检验需要检验文件类型,通过后缀方式不安全!我在网上找了一个方法:

  应用时出错:未能找到文件“C:\WINDOWS\system32\1.jpg”。 怎么解决呢?

http://www.cnblogs.com/jueban/archive/2009/03/06/933121.html

    //真正判断文件类型的关键函数
    public static bool IsAllowedExtension(FileUpload hifile)
    {
        //System.IO.FileStream fs = new System.IO.FileStream(hifile, System.IO.FileMode.Open, System.IO.FileAccess.Read);
        System.IO.FileStream fs = new System.IO.FileStream(hifile.PostedFile.FileName, System.IO.FileMode.Open, System.IO.FileAccess.Read);
        System.IO.BinaryReader r = new System.IO.BinaryReader(fs);
        string fileclass = "";
        byte buffer;
        try
        {
            buffer = r.ReadByte();
            fileclass = buffer.ToString();
            buffer = r.ReadByte();
            fileclass += buffer.ToString();

        }
        catch
        {

        }
        r.Close();
        fs.Close();
        if (fileclass == "255216" || fileclass == "7173")//说明255216是jpg;7173是gif;6677是BMP,13780是PNG;7790是exe,8297是rar
        {
            return true;
        }
        else
        {
            return false;
        }

钢的锅的主页 钢的锅 | 初学一级 | 园豆:10
提问于:2010-07-31 17:22
< >
分享
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册