public IHttpActionResult PostMedia(int id,Media media) { if (!ModelState.IsValid) { return BadRequest(ModelState); } Media newImage = new Media(); int times = db.Media.Where(d => d.Update_ID == id).Count(); // this is working if (times < 5) { // HttpPostedFileBase file = Request.Files["OriginalLocation"]; // newImage.MediaName = media.MediaName; // newImage.AlternateText = media.AlternateText; // newImage.VideoUrl = media.VideoUrl; // newImage.Discriminator = media.Discriminator; // //Here's where the ContentType column comes in handy. By saving // // this to the database, it makes it easier to get it back // // later when trying to show the image. // newImage.ContentType = file.ContentType; // Int32 length = file.ContentLength; // //This may seem odd, but the fun part is that if // // I didn't have a temp image to read into, I would // // get memory issues for some reason. Something to do // // with reading straight into the object's ActualImage property. // byte[] tempImage = new byte[length]; // file.InputStream.Read(tempImage, 0, length); // newImage.ImageData = tempImage; // newImage.Update_ID = id; // db.Media.Add(newImage); // db.SaveChanges(); //} Media up = new Media() { MediaName = media.MediaName, Update_ID = id // MediaUpdateDate = DateTime.Now }; db.Media.Add(up); db.SaveChanges(); } return CreatedAtRoute("DefaultApi", new { id = media.MediaID }, media); }
public string PostMedia(int id) { Media newImage = new Media(); int times = db.Media.Where(d => d.Update_ID == id).Count(); // this is working if (times < 5) { var file = HttpContext.Current.Request.Files.Count > 0 ? HttpContext.Current.Request.Files[0] : null; if (file != null && file.ContentLength > 0) { var fileName = Path.GetFileName(file.FileName); var path = Path.Combine( HttpContext.Current.Server.MapPath("~/uploads"), fileName ); newImage.MediaName = file.FileName; newImage.ContentType = file.ContentType; Int32 length = file.ContentLength; byte[] tempImage = new byte[length]; file.InputStream.Read(tempImage, 0, length); newImage.ImageData = tempImage; newImage.Update_ID = id; newImage.Discriminator = "image"; db.Media.Add(newImage); db.SaveChanges(); file.SaveAs(path); return file != null ? "/uploads/" + file.FileName : null; } else return null; } return null; }
//// GET api/Media/5 //[ResponseType(typeof(Media))] //public IHttpActionResult GetMedia(int id) //{ //original method // Media media = db.Media.Find(id); // if (media == null) // { // return NotFound(); // } // return Ok(media); //} // PUT api/Media/5 public IHttpActionResult PutMedia(int id, Media media) { if (!ModelState.IsValid) { return BadRequest(ModelState); } if (id != media.MediaID) { return BadRequest(); } db.Entry(media).State = EntityState.Modified; try { db.SaveChanges(); } catch (DbUpdateConcurrencyException) { if (!MediaExists(id)) { return NotFound(); } else { throw; } } return StatusCode(HttpStatusCode.NoContent); }
// PUT api/Media/5 public IHttpActionResult PutVideo(int id, Media media) { if (!ModelState.IsValid) { return BadRequest(ModelState); } Media newImage = new Media(); int times = db.Media.Where(d => d.Update_ID == id).Count(); // this is working if (times < 5) { newImage.MediaName = media.MediaName; newImage.VideoUrl = media.VideoUrl; newImage.Update_ID = id; newImage.Discriminator = "video"; db.Media.Add(newImage); try { db.SaveChanges(); } catch (DbUpdateException) { throw; } } return CreatedAtRoute("DefaultApi", new { id = media.MediaID }, media); }