首页 新闻 会员 周边

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

0
悬赏园豆:10 [已解决问题] 解决于 2018-03-03 20:50

检测是否是图片代码:

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?

DZCMS的主页 DZCMS | 初学一级 | 园豆:5
提问于:2018-02-08 22:19
< >
分享
最佳答案
0

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

收获园豆:10
dudu | 高人七级 |园豆:30994 | 2018-02-08 22:32

 还是不行。

把r.Close(); fs.Close();注释掉就可以了。

DZCMS | 园豆:5 (初学一级) | 2018-02-09 13:01
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册