public static Attachment Add(OUContext db, string mastertype, int masterid, HttpPostedFileBase postFile) { Attachment file = new Attachment(); file.FileName = Path.GetFileName(postFile.FileName); file.ContentType = postFile.ContentType; file.Length = postFile.ContentLength; file.Id = Guid.NewGuid(); file.MasterId = masterid; file.MasterType = mastertype; file.Contents = new byte[postFile.ContentLength]; postFile.InputStream.Read(file.Contents, 0, postFile.ContentLength); db.Attachments.Add(file); return file; }
public ActionResult Upload(string mastertype,int masterid,FormCollection collection) { int fileNum = 0; int.TryParse(Request["fileNum"], out fileNum); List<HttpPostedFileBase> postFiles = new List<HttpPostedFileBase>(); for (int i = 0; i < fileNum; i++) { HttpPostedFileBase postFile = Request.Files["file" + i.ToString()]; if (postFile != null && postFile.ContentLength > 0) { Attachment file = new Attachment(); file.FileName = Path.GetFileName(postFile.FileName); file.ContentType = postFile.ContentType; file.Length = postFile.ContentLength; file.Id = Guid.NewGuid(); file.MasterId = masterid; file.MasterType = mastertype; file.Contents=new byte[postFile.ContentLength]; postFile.InputStream.Read(file.Contents, 0, postFile.ContentLength); db.Attachments.Add(file); } } db.SaveChanges(); return Redirect("~/content/close.htm"); }