Пример #1
0
        public IHttpActionResult PutImage(int id, Image image)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != image.Id)
            {
                return(BadRequest());
            }

            db.Entry(image).State = EntityState.Modified;

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!ImageExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(StatusCode(HttpStatusCode.NoContent));
        }
Пример #2
0
        public IHttpActionResult GetImage(int id)
        {
            Image image = db.Images.Find(id);

            if (image == null)
            {
                return(NotFound());
            }

            return(Ok(image));
        }
Пример #3
0
        public IHttpActionResult DeleteImage(int id)
        {
            Image image = db.Images.Find(id);

            if (image == null)
            {
                return(NotFound());
            }

            db.Images.Remove(image);
            db.SaveChanges();

            return(Ok(image));
        }
Пример #4
0
        public async Task <IHttpActionResult> PostImage()
        {
            /*
             * string fileName = Path.GetFileNameWithoutExtension(image.ImageFile.FileName);
             * string extension = Path.GetExtension(image.ImageFile.FileName);
             * fileName = fileName + Guid.NewGuid() + extension;
             * image.Path = "~/Image/" + fileName;
             *
             * fileName = Path.Combine(System.Web.HttpContext.Current.Server.MapPath("~/Image/"), fileName);
             *
             * image.ImageFile.SaveAs(fileName);*/


            //Agr Req multiform nhe hui tow Ye Error dega.
            if (!Request.Content.IsMimeMultipartContent())
            {
                throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
            }


            //Give the Path to Image.
            var root     = HttpContext.Current.Server.MapPath("~/documents");
            var provider = new MultipartFormDataStreamProvider(root);

            try
            {
                await Request.Content.ReadAsMultipartAsync(provider);

                foreach (MultipartFileData file in provider.FileData)
                {
                    //string email = provider.FormData.GetValues("image").SingleOrDefault();

                    //Replace / from file name
                    string name = file.Headers.ContentDisposition.FileName.Replace("\"", "");

                    //Create file name with Guid
                    string newfileName = Guid.NewGuid() + Path.GetExtension(name);

                    //Move file from current location to Target Folder
                    File.Move(file.LocalFileName, Path.Combine(root, newfileName));

                    var s = root + "\\" + newfileName;
                    //creating Image Blob as Byte to shown in Front End.
                    byte[] blobByte;
                    using (var fs = new FileStream(s, FileMode.Open, FileAccess.Read))
                    {
                        blobByte = new byte[fs.Length];
                        fs.Read(blobByte, 0, Convert.ToInt32(fs.Length));
                    }
                    //Create new Image Instance
                    Image img = new Image();

                    // img.Name = name;
                    img.Path      = root + "\\" + newfileName;
                    img.ImageBlob = blobByte;
                    img.Name      = newfileName;

                    //Below lines form value input.["name"] is the form field in Angular
                    //img.Name = HttpContext.Current.Request["name"];
                    //img.Path = HttpContext.Current.Request["password"];

                    //saving in DB
                    MyOdeToFoodDbContext db = new MyOdeToFoodDbContext();
                    db.Images.Add(img);
                    db.SaveChanges();

                    //return Content(HttpStatusCode.Created,  img.Name+"\""+"Image Uploaded");
                }
            }

            catch (Exception ex)
            {
                throw new HttpException("Error-" + ex.Message);
            }

            return(Content(HttpStatusCode.Created, "Image Uploaded in Server"));

            //return Content(HttpStatusCode.Created, "Image Uploaded in Server");
            //return Created( Ok, new { });

            // return Content(HttpStatusCode.OK);

            /*
             * string imageName = null;
             * var httpRequest = HttpContext.Current.Request;
             * var postedFile = httpRequest.Files["Image"];
             *
             * imageName = Path.GetFileNameWithoutExtension(postedFile.FileName).Replace(" ","-").Replace("/"," ");
             * imageName = imageName + Guid.NewGuid() + Path.GetExtension(postedFile.FileName);
             *
             * var filePath = HttpContext.Current.Server.MapPath("~/Image/" + imageName);
             * postedFile.SaveAs(filePath);
             *
             * MyOdeToFoodDbContext db = new MyOdeToFoodDbContext();
             *
             * db.Images.Add(img);
             * db.SaveChanges();
             *
             *
             * if (!ModelState.IsValid)
             * {
             *  return BadRequest(ModelState);
             * }
             *
             * db.Images.Add(img);
             * db.SaveChanges();
             *
             * //return CreatedAtRoute(" ", new { id = img.Id }, img);
             */
        }