Пример #1
0
        public Response GetAllFile()
        {
            List <TblFileDetail> list = new List <TblFileDetail>();

            using (ApitemplatereportContext context = new ApitemplatereportContext())
            {
                //TblFileDetailFileId

                var allFiles = context.TblFileDetails.Select(x => x).ToList();
                if (allFiles.Count != 0)
                {
                    list = allFiles;
                    var jsonList = JsonConvert.SerializeObject(list);
                    return(new Response {
                        StatusCode = 200, Message = "All File", Content = jsonList
                    });
                }
            }

            list.Add(new TblFileDetail());
            var errList = JsonConvert.SerializeObject(list);

            return(new Response {
                StatusCode = 200, Message = "All File", Content = errList
            });
        }
Пример #2
0
        public Response Delete([FromBody] string filename)
        {
            using (ApitemplatereportContext context = new ApitemplatereportContext())
            {
                TblFileDetail file = context.TblFileDetails.Where(file => file.Filename == filename).FirstOrDefault();
                if (context.TblFileDetails.Contains(file))
                {
                    if (DeleteFile(filename) == true)
                    {
                        context.TblFileDetails.Remove(file);
                        context.SaveChanges();
                        return(new Response
                        {
                            StatusCode = 200,
                            Content = $"{file.Filename} is deleted",
                            Message = "Delete Success"
                        });
                    }
                }
            }

            return(new Response {
                StatusCode = 404, Content = $"Error when delete {filename}", Message = "Delete Failed"
            });
        }
Пример #3
0
        public async System.Threading.Tasks.Task <IActionResult> Download([FromBody] FileDownload filename)
        {
            using (ApitemplatereportContext context = new ApitemplatereportContext())
            {
                TblFileDetail file = context.TblFileDetails.Where(file => file.Filename == filename.filename).FirstOrDefault();
                if (file != null)
                {
                    var path = _webHostEnvironment.WebRootPath + "\\Template\\" + file.Filename;

                    try
                    {
                        var memory = new MemoryStream();
                        using (var stream = System.IO.File.OpenRead(path))
                            await stream.CopyToAsync(memory);
                        memory.Position = 0;

                        return(new FileStreamResult(memory, "application/octec-stream"));
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex);
                    }
                }
            }

            return(NotFound());
        }
        public TblUser Authenticate(string username, string password)
        {
            var user = new ApitemplatereportContext().TblUsers
                       .Where(u => u.Username == username && u.Password == password).FirstOrDefault();

            // return null if user not found
            if (user == null)
            {
                return(null);
            }

            return(user);
        }
Пример #5
0
        public Response Upload()
        {
            if (Request.HasFormContentType)
            {
                var form = Request.Form;
                foreach (var formFile in form.Files)
                {
                    var targetDirectory = Path.Combine(_webHostEnvironment.WebRootPath, "Template");
                    var fileName        = formFile.FileName;
                    if (Path.GetExtension(fileName) == ".docx" || Path.GetExtension(fileName) == ".doc")
                    {
                        using (ApitemplatereportContext context = new ApitemplatereportContext())
                        {
                            TblFileDetail file = context.TblFileDetails.Where(file => file.Filename == fileName).FirstOrDefault();
                            if (file != null)
                            {
                                fileName = formFile.FileName + "_" + GetTimestamp(DateTime.Now)
                                           + Path.GetExtension(formFile.FileName);
                            }

                            var savePath = Path.Combine(targetDirectory, fileName);

                            using (var fileStream = new FileStream(savePath, FileMode.Create))
                                formFile.CopyTo(fileStream);

                            var fileUrl = Url.Content(Path.Combine("~/Template/", fileName));
                            context.TblFileDetails.Add(new TblFileDetail {
                                Filename = fileName, Fileurl = fileUrl
                            });
                            context.SaveChanges();
                        }

                        return(new Response {
                            StatusCode = 200, Content = fileName + "is Uploaded", Message = "File Uploaded"
                        });
                    }
                }
            }

            return(new Response {
                StatusCode = 404, Content = "File type is not support!!", Message = "Upload Error"
            });
        }
Пример #6
0
        public IActionResult Index(ListFile model)
        {
            string token    = HttpContext.Session.GetString("Session.Token");
            string username = HttpContext.Session.GetString("Session.Username");
            string password = HttpContext.Session.GetString("Session.Password");

            if (string.IsNullOrEmpty(token) || string.IsNullOrEmpty(username) || string.IsNullOrEmpty(password))
            {
                HttpContext.Session.Clear();
                return(RedirectToAction("Login", "Accounts"));
            }

            TblUser u             = new ApitemplatereportContext().TblUsers.Where(x => x.Username == username).FirstOrDefault();
            var     validatetoken = new RequestHelper(factory).PostRequest("api/Token/ValidateToken", token, u);

            if (validatetoken.StatusCode == 200)
            {
                var    us      = JsonConvert.DeserializeObject <UserLogin>(validatetoken.Content.ToString());
                string newPass = new HashMd5().CreateMD5Hash(us.Password);
                if (newPass == password)
                {
                    var response = new RequestHelper(factory).GetRequest("api/Files/GetAllFile", token);
                    if (response.StatusCode == 200)
                    {
                        model.FileList = JsonConvert.DeserializeObject <List <TblFileDetail> >(response.Content.ToString());
                        return(View(model));
                    }
                    else
                    {
                        HttpContext.Session.Clear();
                        return(RedirectToAction("Login", "Accounts"));
                    }
                }
            }

            return(View(null));
        }