public object Upload(HttpContext context) { if (context.Request.Files.Count < 1) { throw new ArgumentException("必须指定上传的文件内容"); } long Position = long.Parse(context.Request["Position"]); long FileSize = long.Parse(context.Request["FileSize"]); string fileId = context.Request["fileId"]; string fullName = context.Request["WebkitRelativePath"]; fullName = String.IsNullOrWhiteSpace(fullName) ? context.Request.Files[0].FileName : fullName; var rootFolder = this.GetRootFolder(context); var filePath = System.IO.Path.Combine(rootFolder, fullName); Model.FileEntity fe = null; if (Position == 0 && string.IsNullOrEmpty(fileId)) { if (System.IO.File.Exists(filePath)) { throw new ArgumentException($"文件已经存在:{fullName}"); } fe = new Model.FileEntity() { ClientHashValue = "md5", FullName = fullName.Replace("/", "\\"), Size = FileSize, UploadStepValue = Model.UploadStep.进行中 }; this.dbcontext.FileEntity.Add(fe); this.dbcontext.SaveChanges(); } var fileFolder = System.IO.Path.GetDirectoryName(filePath); if (!System.IO.Directory.Exists(fileFolder)) { System.IO.Directory.CreateDirectory(fileFolder); } using (var filestream = new System.IO.FileStream(filePath, System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.ReadWrite, System.IO.FileShare.ReadWrite)) { filestream.Position = Position; context.Request.Files[0].InputStream.CopyTo(filestream); filestream.Flush(true); if (!string.IsNullOrEmpty(fileId)) { context.Cache[fileId] = filestream.Position - 1; } } return(new { msg = "ok", fe?.Id }); }
public object Complete(HttpContext context) { var fileid = context.Request["fileid"]; var fe = new Model.FileEntity() { Id = int.Parse(fileid), UploadStepValue = Model.UploadStep.完成 }; this.dbcontext.Configuration.ValidateOnSaveEnabled = false; var fewrapper = this.dbcontext.Entry(fe); fewrapper.State = System.Data.Entity.EntityState.Unchanged; fewrapper.Property(x => x.UploadStepValue).IsModified = true; this.dbcontext.SaveChanges(); return(new { msg = "ok" }); }