首页 新闻 会员 周边 捐助

net core 2.0 mvc FileStream 求教

0
悬赏园豆:5 [已解决问题] 解决于 2017-12-23 03:53
public class GetVedioModel:GetClassInfoModel
    {
        public int blocksize { get; set; }
        public int currentblock { get; set; }
        public int currentblocksize { get; set; }
    }
[HttpPost]
        public async Task<JsonResult> GetVedioBlock(){
            using (var sr = new StreamReader(Request.Body)){
                var json = sr.ReadToEnd();
                GetVedioModel model = JsonConvert.DeserializeObject<GetVedioModel>(json);
                var filePath = MobileDataBaseModel.GetVedioInfo(model);
                if (filePath == null)
                {
                    BaseModel bm = new BaseModel();
                    bm.resualt = false;
                    bm.errorcode = 2;
                    return Json(JsonConvert.SerializeObject(bm));
                }
                Models.MobileModel.BackModel.VedioModel vm = new Models.MobileModel.BackModel.VedioModel();
                vm.current = model.currentblock;
                vm.data = await readFile(model, filePath);
                if(vm.data == null){
                    vm.resualt = false;
                    vm.errorcode = 5;
                }
                else{
                    vm.resualt = true;
                }
                return Json(JsonConvert.SerializeObject(vm));
            }
        }

        private async Task<string> readFile(GetVedioModel model,string path){
            using (FileStream fs = new FileStream(_hostingEnvironment.WebRootPath+path,FileMode.Open,FileAccess.Read,FileShare.Read,model.currentblocksize,true))
            {
                fs.Seek(0,SeekOrigin.Begin);
                byte[] buff = new byte[model.currentblocksize];
                int s = await fs.ReadAsync(buff, (model.currentblock - 1) * model.blocksize, model.currentblocksize);

                if (s > 0)
                {
                    return Encoding.UTF8.GetString(buff);
                }
                return null;
            }
        }

FileStream只能读一次文件  怎么搞?

雨过平静的主页 雨过平静 | 菜鸟二级 | 园豆:203
提问于:2017-12-11 15:13
< >
分享
最佳答案
0

读完后如果FileStream没有关闭,fs.Seek(0,SeekOrigin.Begin)之后应该可以继续读

收获园豆:5
dudu | 高人七级 |园豆:30902 | 2017-12-11 17:52

找到方法了 把using干掉 最后一次再close

雨过平静 | 园豆:203 (菜鸟二级) | 2017-12-23 03:53
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册