首页 新闻 会员 周边

文件流Close问题,请高手答疑

0
悬赏园豆:10 [已关闭问题] 关闭于 2017-05-11 13:26
上传文件时的问题,一开始我以为是传递单个file引发的问题 ,后来发现是close的问题,如下:
复制代码
复制代码
 
//定义了两个file对象
HttpPostedFileBase file = Request.Files["FileData"]; HttpPostedFileBase file_check = Request.Files["FileData"];
if (file == null || file.ContentLength == 0) { return Json(new AjaxResponse() { Success = false, Message = "获取上传文件失败", Data = "" }); }
//传递其中一个file_check过去进行类型检测
if (!Utils.CheckFileExtension(file_check, new string[] { "doc", "docx" })) { return Json(new AjaxResponse() { Success = false, Message = "文件格式错误", Data = "" }); }
复制代码
复制代码

 

我定义了两个 file对象,获取的一样的内容,传递其中一个到CheckFileExtension中去进行类型验证:
 
public static bool CheckFileExtension(HttpPostedFileBase file_check, string[] exts)
        {
            bool ret = false;
            Stream fs = file_check.InputStream;
            BinaryReader r = new BinaryReader(fs);
            string fileclass = "";
            byte buffer;
            try
            {
                buffer = r.ReadByte();
                fileclass = buffer.ToString();
                buffer = r.ReadByte();
                fileclass += buffer.ToString();
            }
            catch
            {
                return false;
            }
           //close之后 两个file对象 都没有内容了。...
            r.Close();
            fs.Close();
            foreach(string ext in exts)
            {
                if(GetFileExtension(ext).Equals(fileclass))
                {
                    ret = true;
                    break;
                }
            }
            return ret;
        }
        public static string GetFileExtension(string ext)
        {
            switch(ext.ToLower())
            {
                case "jpg": return "255216";
                case "jpeg": return "255216";
                case "gif": return "7173";
                case "bmp": return "6677";
                case "png": return "13780";
                case "com": return "7790";
                case "exe": return "7790";
                case "dll": return "7790";
                case "rar": return "8297";
                case "xml": return "6063";
                case "html": return "6033";
                case "aspx": return "239187";
                case "asp": return "239187";
                case "cshtml": return "239187";
                case "css": return "6499";
                case "cs": return "117115";
                case "js": return "4742";
                case "sql": return "1310";
                case "bat": return "64101";
                case "btseed": return "10056";
                case "rdp": return "255254";
                case "psd": return "5666";
                case "pdf": return "3780";
                case "chm": return "7384";
                case "log": return "70105";
                case "reg": return "8269";
                case "hlp": return "6395";
                case "doc": return "208207";
                case "xls": return "208207";
                case "ppt": return "208207";
                case "wps": return "208207";
                case "docx": return "8075";
                case "xlsx": return "8075";
                case "pptx": return "8075";
                case "zip": return "8075";
                case "accdb": return "01";
                case "mdb": return "01";
                default:return "";
            }
        }
View Code

 

 close之后 两个file对象 都没有内容了。...这是怎么回事.

 
如上:请高手答疑,为什么我close掉之后导致我外面定义的另一个fileContent也没了? 难道他们操作的同一个文件流?
 
 
问题补充:

HttpPostedFile file = Request.Files[0];

变量file只是对文件的引用,对file的任何操作都直接影响到已经上载的文件

 

看来得先保存在操作了 或者  不close,外面操作完了再close

 

2016-07-01: 发现不close 可以上传文件 ,但是 文件已被损坏......

李伟-CodeL的主页 李伟-CodeL | 初学一级 | 园豆:87
提问于:2016-06-30 18:25
< >
分享
所有回答(1)
0
                buffer = r.ReadByte();
                fileclass = buffer.ToString();
                buffer = r.ReadByte();
                fileclass += buffer.ToString();        

为什么要ReadByte()两次呢?

蝌蝌 | 园豆:158 (初学一级) | 2016-07-01 11:18

因为是通过文件的前两个字节判断的文件类型,如果不readbyte两次,也可以用循环,按下标取两个

支持(0) 反对(0) 李伟-CodeL | 园豆:87 (初学一级) | 2016-07-01 11:22
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册