之前简单判断了后缀名,现在判断了它的类型,在本机可以 但放远程服务器怎么不行了,大家来分析看是不是这个代码有问题?难道是服务器不允许?
//判断真实类型
        System.IO.FileStream fs = new System.IO.FileStream(FileUpload1.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
        {
            Filets1.Text = "(类型须为jpg或gif)";
            return;
        }
        string Path = Server.MapPath(("upload\\") + "\\" + dianming.Text + "\\");
        DirectoryInfo directory = new DirectoryInfo(Path);
        if (!directory.Exists) //目录不存在创建
            directory.Create();
        string savePath = Server.MapPath(("uploadyh\\") + "\\" + dianming.Text + "\\" + "1.jpg");//Server.MapPath 获得虚拟服务器相对路径,但savepath是绝对路径,\\是转移字符\
        string xdpath = "upload\\" + dianming.Text + "\\" + "1.jpg";
        CatalogAccess.fillhwusertp1(Session["yonghu"].ToString(), xdpath.Trim());//1表示第一个图片 对应表中第一个路径存放
        FileUpload1.SaveAs(savePath);
        Filets1.Text = "(上传成功)";

解决方案 »

  1.   

    FileUpload1.PostedFile.FileName
    这么写是完全错误的。网上的这个代码不知道作者怎么想的。
    FileUpload1.PostedFile.FileName是客户端的路径,服务器上能找到才怪!!你应该先保存成tmp文件,根据判断再决定是否删除它String x = FileUpload1.PostedFile.SaveAs("~/" + Guid.NextGuid().ToString() + ".tmp");
    System.IO.FileStream fs = new System.IO.FileStream( x, System.IO.FileMode.Open, System.IO.FileAccess.Read);
      

  2.   

     System.IO.FileStream fs = new System.IO.FileStream(FileUpload1.PostedFile.FileName, System.IO.FileMode.Open, System.IO.FileAccess.Read);

    楼上是对的 你这句话是有问题的 一般都直接SaveAs(path)了PostedFile.InputStream 返回的是Stream 不知道能不能判断 楼上的试一下 然后结果需要 通知一下啦 谢谢了