public async Task <IActionResult> SaveToDB(IFormFile postedFile)
        {
            TblFile myFile = new TblFile();

            //var basePath = Path.Combine(Directory.GetCurrentDirectory() + "\\Files\\");
            //bool basePathExists = System.IO.Directory.Exists(basePath);
            //if (!basePathExists) Directory.CreateDirectory(basePath);
            var fileName = Path.GetFileNameWithoutExtension(postedFile.FileName);
            //var filePath = Path.Combine(basePath, postedFile.FileName);
            var extension = Path.GetExtension(postedFile.FileName);

            using (var dataStream = new MemoryStream())
            {
                await postedFile.CopyToAsync(dataStream);

                myFile.Data = dataStream.ToArray();
            }
            myFile.ContentType = extension;
            myFile.Name        = fileName;
            ImageStoreContext db = new ImageStoreContext();

            db.TblFiles.Add(myFile);
            db.SaveChanges();
            return(RedirectToAction("Index"));
        }
Пример #2
0
 public ActionResult TblFileCreate(HttpPostedFileBase file)
 {
     try
     {
         if (file != null && file.ContentLength > 0)
         {
             var filename = Path.GetFileName(file.FileName);
             var path     = Path.Combine(Server.MapPath("~/App_Data/Uploads"), filename);
             file.SaveAs(path);
             TblFile o = new TblFile();
             o.Name       = filename;
             o.Directory  = "~/App_Data/Uploads/";
             o.CreateUser = GetUserName();
             o.CreateDate = DateTime.Now;
             new TblFileDao().Create(o);
             SetAlert("tải thành công", "success");
             return(RedirectToAction("TblFileIndex"));
         }
         SetAlert("tải thành công", "success");
         return(RedirectToAction("TblFileIndex"));
     }
     catch (Exception ex)
     {
         logger.Info(ControllerName + "::TblClassListCreate::" + ex.Message);
         return(RedirectToAction("Index", "Error"));
     }
 }
Пример #3
0
 public IActionResult CreateNewFile(TblFile model)
 {
     try
     {
         return(Ok(fp.CreateNewFileNo(model)));
     }
     catch (Exception ex)
     {
         Exceptions(ex);
         return(StatusCode(500));
     }
 }
 public int CreateNewFileNo(TblFile model)
 {
     try
     {
         return(file.CreateNewFileNo(model));
     }
     catch (Exception ex)
     {
         StaticHelper.LogException(path: up.GetLogFilePath(), errorMessage: ex.Message, methodName: $"Class Name: {nameof(FilePresenter)} - Method name:  {nameof(CreateNewFileNo)}", stackTrace: ex.StackTrace);
         return(0);
     }
 }
 public int CreateNewFileNo(TblFile model)
 {
     try
     {
         dbContext.Add(model);
         return(dbContext.SaveChanges());
     }
     catch (Exception ex)
     {
         StaticHelper.LogException(path: up.GetLogFilePath(), errorMessage: ex.Message, methodName: $"Repository name: {nameof(EmployeeRepository)} - Method name:  {nameof(CreateNewFileNo)}", stackTrace: ex.StackTrace);
         return(0);
     }
 }
Пример #6
0
        public async Task <IActionResult> OnGetAsync(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            TblFile = await _context.TblFile.FirstOrDefaultAsync(m => m.ID == id);

            if (TblFile == null)
            {
                return(NotFound());
            }
            return(Page());
        }
        public async Task <IActionResult> OnPostAsync(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            TblFile = await _context.TblFile.FindAsync(id);

            if (TblFile != null)
            {
                _context.TblFile.Remove(TblFile);
                await _context.SaveChangesAsync();
            }

            return(RedirectToPage("./Index"));
        }
Пример #8
0
        public JsonResult TblFileDelete(long id)
        {
            TblFile o = new TblFile();

            o    = new TblFileDao().FindById(id);
            o.Id = id;
            FileInfo Dfile = new FileInfo(HttpContext.Server.MapPath("~/App_Data/Uploads/" + o.Name));

            if (System.IO.File.Exists(Dfile.ToString()))
            {
                try
                {
                    System.IO.File.Delete(Dfile.ToString());
                }
                catch (System.IO.IOException ex)
                {
                    logger.Info("::DeleteFile::" + ex.Message);
                }
            }
            new TblFileDao().Delete(o);
            return(Json(JsonRequestBehavior.AllowGet));
        }
Пример #9
0
 /// <summary>
 /// Author: Phạm Huy Hùng
 /// Todo: tạo đối tượng mới
 /// </summary>
 /// <param name=""></param>
 /// <returns></returns>
 public void Create(TblFile o)
 {
     try
     {
         using (TkSchoolDbContext db = new TkSchoolDbContext())
         {
             db.TblFiles.Add(o);
             db.SaveChanges();
         }
     }
     catch (Exception ex)
     {
         if (ex.InnerException == null)
         {
             throw new Exception("TblFileDao::Create::" + ex.Message);
         }
         else
         {
             throw new Exception("TblFiletDao::Create::" + ex.InnerException.Message);
         }
     }
 }
Пример #10
0
 /// <summary>
 /// Author: Phạm Huy Hùng
 /// Todo: xóa đối tượng
 /// </summary>
 /// <param name="o"></param>
 /// <returns></returns>
 public void Delete(TblFile o)
 {
     try
     {
         using (TkSchoolDbContext db = new TkSchoolDbContext())
         {
             var res = db.TblFiles.Where(x => x.Id == o.Id).SingleOrDefault();
             db.TblFiles.Remove(res);
             db.SaveChanges();
         }
     }
     catch (Exception ex)
     {
         if (ex.InnerException == null)
         {
             throw new Exception("TblFileDao::Delete::" + ex.Message);
         }
         else
         {
             throw new Exception("TblFiletDao::Delete::" + ex.InnerException.Message);
         }
     }
 }