上传检测是否是图片文件后,文件的长度变为0

2019-01-03 03:36发布

检测是否是图片代码:

public static bool IsImage(HttpPostedFileBase hpf)
        {
            bool result = false;
            Stream fs = hpf.InputStream;
            BinaryReader r = new BinaryReader(fs);
            string fileclass = "";
            byte buffer;
            try
            {
                buffer = r.ReadByte();
                fileclass = buffer.ToString();
                buffer = r.ReadByte();
                fileclass += buffer.ToString();
            }
            catch (Exception ex){ }
            if (fileclass == "255216" || fileclass == "7173" || fileclass == "13780" || fileclass == "6677")
            {
                result = true;
            }
            else
            {
                result = false;
            }
            r.Close();
            fs.Close();
            return result;
        }

上传代码:

 HttpPostedFileBase file = Request.Files["imgFile"];
 bool isimage = Common.Upload.IsImage(file);
//为什么到这里后文件的长度就变为了0,从而导致下面上传失败?
string fileurl = Common.Upload.UploadFile(file, type, uploaddir, uploadconfig.custompath, width, height);

为什么运行完Common.Upload.IsImage(file)后,文件的长度就变为了0?

1条回答
SAY GOODBYE
2楼-- · 2019-01-03 04:10

bool isimage = Common.Upload.IsImage(file); 之后添加 file.InputStream.Positon = 0;

查看更多
登录 后发表回答