Exemplo n.º 1
0
        // POST api/File
        public IHttpActionResult PostFile(TestAppWebAPI.Models.File file)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (file.FolderId != 0)
            {
                db.Files.Add(file);
                db.SaveChanges();

                db.Entry(file).Reference(X => X.Folder).Load();

                var DTO = new FileDTO()
                {
                    Id            = file.ID,
                    Name          = file.Name,
                    UserName      = file.Folder.User.Email,
                    FolderId      = file.FolderId,
                    FolderName    = file.Folder.Name,
                    FileExtension = file.FileExtension,
                    Size          = file.Size,
                    Path          = file.Path
                };

                return(CreatedAtRoute("DefaultApi", new { ID = file.ID }, DTO));
            }
            else
            {
                return(Ok(file));
            }
        }
Exemplo n.º 2
0
        // PUT api/File/5
        public IHttpActionResult PutFile(int id, TestAppWebAPI.Models.File file)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != file.ID)
            {
                return(BadRequest());
            }

            string path     = ConfigurationManager.AppSettings["pathUploadFiles"];
            string rootTemp = System.Web.HttpContext.Current.Server.MapPath(path) + "Temp/";
            string root     = System.Web.HttpContext.Current.Server.MapPath(path) + "File_" + file.ID.ToString() + "/";

            DirectoryInfo diTemp = Directory.CreateDirectory(rootTemp);
            DirectoryInfo di     = Directory.CreateDirectory(root);

            foreach (FileInfo _file in di.GetFiles())
            {
                _file.Delete();
            }

            foreach (FileInfo _file in diTemp.GetFiles())
            {
                root += _file.Name;
                _file.MoveTo(root);

                file.Name          = _file.Name;
                file.FileExtension = _file.Extension;
                file.Size          = (int)_file.Length;
                file.Path          = "~" + _file.DirectoryName.Substring(_file.DirectoryName.IndexOf("\\App_Data"));
            }

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

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

            //return StatusCode(HttpStatusCode.NoContent);
            return(Ok("Файлы загружены в " + root));
        }
Exemplo n.º 3
0
        public IHttpActionResult DeleteFile(int id)
        {
            TestAppWebAPI.Models.File file = db.Files.Find(id);
            if (file == null)
            {
                return(NotFound());
            }

            db.Files.Remove(file);
            db.SaveChanges();

            return(Ok(file));
        }