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只能读一次文件 怎么搞?
读完后如果FileStream没有关闭,fs.Seek(0,SeekOrigin.Begin)
之后应该可以继续读
找到方法了 把using干掉 最后一次再close