Пример #1
0
        //[RequestSizeLimit(40000000)]
        //public async Task<IActionResult> AddFile(IFormFile uploadedFile)
        public async Task <IActionResult> AddFile(AddFileModelView file)
        {
            //var s=User.FindFirstValue(ClaimTypes.NameIdentifier);
            if (file.uploadedFile != null)
            {
                string ext         = Path.GetExtension(file.uploadedFile.FileName).ToLowerInvariant();
                var    types       = GetMimeTypes();
                string newName     = DateTime.Now.ToString("M-d-yyyy_hh-mm-ss") + ext;
                string path        = "";
                string MonthNumber = DateTime.Now.Month.ToString();
                string YearNumber  = DateTime.Now.Year.ToString();

                DirectoryInfo directory = new DirectoryInfo(_appEnvironment.WebRootPath + @"\Files\" + YearNumber + @"\" + MonthNumber);
                if (directory.Exists)
                {
                    path = "\\Files\\" + YearNumber + @"\" + MonthNumber + @"\" + newName;
                }
                else
                {
                    directory.Create();
                    path = "\\Files\\" + YearNumber + @"\" + MonthNumber + @"\" + newName;
                }
                //DirectoryInfo directory=new DirectoryInfo("/Files/"); //Linux
                // путь к папке Files
                //path = "\\Files\\" + newName;


                //string s=pDF.ReadPdfFile(path);
                // сохраняем файл в папку Files в каталоге wwwroot

                // Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
                //                             Encoding utf8 = Encoding.GetEncoding("utf-8");
                //                             Encoding win1251 = Encoding.GetEncoding("windows-1251");
                //                             byte[] utf8Bytes = win1251.GetBytes(pDF.ReadPdfFile(path));
                //                             byte[] win1251Bytes = Encoding.Convert(win1251, utf8, utf8Bytes);
                //                             string currentText = utf8.GetString(win1251Bytes);



                using (var fileStream = new FileStream(_appEnvironment.WebRootPath + path, FileMode.Create))
                {
                    await file.uploadedFile.CopyToAsync(fileStream);
                }
                string      userId  = HttpContext.User.FindFirst(ClaimTypes.NameIdentifier).Value.ToString();
                LibDocument newfile = new LibDocument();

                switch (ext)
                {
                case ".pdf":


                    //PDFParser pDF = new PDFParser(_appEnvironment);
                    //Task<string> pdfText=Task.Factory.StartNew(()=>pDF.ReadPdfFile(path));
                    newfile = new LibDocument {
                        Name           = file.Name,
                        Path           = path,
                        Desc1          = file.Desc1,
                        Desc2          = "",//pDF.ReadPdfFile(path)
                        UserId         = Int32.Parse(userId),
                        CategoryId     = file.categories,
                        DbName         = newName,
                        Published      = file.Published,
                        AccessLinkOnly = file.AccessLinkOnly,
                        ContentType    = types[ext]
                    };
                    break;

                case ".mp4":
                    newfile = new LibDocument {
                        Name           = file.Name,
                        Path           = path,
                        Desc1          = file.Desc1,
                        Desc2          = "",
                        UserId         = Int32.Parse(userId),
                        CategoryId     = file.categories,
                        DbName         = newName,
                        Published      = file.Published,
                        AccessLinkOnly = file.AccessLinkOnly,
                        ContentType    = types[ext]
                    };
                    break;

                case ".doc":
                case ".docx":
                    DocxParser docx = new DocxParser(_appEnvironment);
                    newfile = new LibDocument {
                        Name           = file.Name,
                        Path           = path,
                        Desc1          = file.Desc1,
                        Desc2          = docx.GetText(path),
                        UserId         = Int32.Parse(userId),
                        CategoryId     = file.categories,
                        DbName         = newName,
                        Published      = file.Published,
                        AccessLinkOnly = file.AccessLinkOnly,
                        ContentType    = types[ext]
                    };
                    break;

                    // case ".xls":
                    // case ".xlsx":
                    //         newfile = new LibDocument {
                    //     Name = file.Name,
                    //     Path = path,
                    //     Desc1 = file.Desc1,
                    //     Desc2 = "",
                    //     UserId=Int32.Parse(userId),
                    //     CategoryId=file.categories,
                    //     DbName=newName,
                    //     Published=file.Published,
                    //     AccessLinkOnly=file.AccessLinkOnly,
                    //     ContentType=types[ext]
                    //         };
                    // break;
                }


                // if(ext==".pdf")
                // {
                //    newfile = new LibDocument {
                //      Name = file.Name,
                //       Path = path,
                //       Desc1 = file.Desc1,
                //        Desc2 = pDF.ReadPdfFile(path),
                //        UserId=Int32.Parse(userId),
                //        CategoryId=file.categories,
                //        DbName=newName,
                //        Published=file.Published,
                //        AccessLinkOnly=file.AccessLinkOnly,
                //        ContentType=types[ext]
                //         };
                // }
                // else if(ext==".mp4")
                // {
                //    newfile = new LibDocument {
                //      Name = file.Name,
                //       Path = path,
                //       Desc1 = file.Desc1,
                //        Desc2 = "",
                //        UserId=Int32.Parse(userId),
                //        CategoryId=file.categories,
                //        DbName=newName,
                //        Published=file.Published,
                //        AccessLinkOnly=file.AccessLinkOnly,
                //        ContentType=types[ext]
                //         };
                // }

                db.Documents.Add(newfile);

                await db.SaveChangesAsync();

                await Task.Factory.StartNew(() => InsertPdfTextToDbAsync(newfile.id, path));

                //int id =file.id;


                //AddToIndexDoc(newfile.id);

                //db.Entry(file).Reload()//Encoding.UTF8

                List <Category> categories = db.Categories.ToList();

                // categories.Insert(0, new Category { Name = "Все", Id = 0 });
                ViewBag.Categories = new SelectList(categories, "Id", "Name");
            }

            return(RedirectToAction("MyFiles", "File"));
        }
Пример #2
0
        public async Task <IActionResult> AddFile(AddFileModelView file)
        {
            if (file.uploadedFile != null)
            {
                string ext         = Path.GetExtension(file.uploadedFile.FileName).ToLowerInvariant();
                var    types       = GetMimeTypes();
                string newName     = DateTime.Now.ToString("M-d-yyyy_hh-mm-ss") + ext;
                string path        = "";
                string MonthNumber = DateTime.Now.Month.ToString();
                string YearNumber  = DateTime.Now.Year.ToString();

                DirectoryInfo directory = new DirectoryInfo(_appEnvironment.WebRootPath + @"\Files\" + YearNumber + @"\" + MonthNumber);
                if (directory.Exists)
                {
                    path = "\\Files\\" + YearNumber + @"\" + MonthNumber + @"\" + newName;
                }
                else
                {
                    directory.Create();
                    path = "\\Files\\" + YearNumber + @"\" + MonthNumber + @"\" + newName;
                }

                using (var fileStream = new FileStream(_appEnvironment.WebRootPath + path, FileMode.Create))
                {
                    await file.uploadedFile.CopyToAsync(fileStream);
                }
                string      userId  = HttpContext.User.FindFirst(ClaimTypes.NameIdentifier).Value.ToString();
                LibDocument newfile = new LibDocument();

                switch (ext)
                {
                case ".pdf":


                    //PDFParser pDF = new PDFParser(_appEnvironment);
                    //Task<string> pdfText=Task.Factory.StartNew(()=>pDF.ReadPdfFile(path));
                    newfile = new LibDocument {
                        Name           = file.Name,
                        Path           = path,
                        Desc1          = file.Desc1,
                        UserId         = Int32.Parse(userId),
                        CategoryId     = file.categories,
                        DbName         = newName,
                        Published      = file.Published,
                        AccessLinkOnly = file.AccessLinkOnly,
                        ContentType    = types[ext]
                    };
                    break;

                case ".mp4":
                    newfile = new LibDocument {
                        Name           = file.Name,
                        Path           = path,
                        Desc1          = file.Desc1,
                        UserId         = Int32.Parse(userId),
                        CategoryId     = file.categories,
                        DbName         = newName,
                        Published      = file.Published,
                        AccessLinkOnly = file.AccessLinkOnly,
                        ContentType    = types[ext]
                    };
                    break;

                case ".doc":
                case ".docx":
                    DocxParser docx = new DocxParser(_appEnvironment);
                    newfile = new LibDocument {
                        Name           = file.Name,
                        Path           = path,
                        Desc1          = file.Desc1,
                        Desc2          = docx.GetText(path),
                        UserId         = Int32.Parse(userId),
                        CategoryId     = file.categories,
                        DbName         = newName,
                        Published      = file.Published,
                        AccessLinkOnly = file.AccessLinkOnly,
                        ContentType    = types[ext]
                    };
                    break;
                }

                db.Documents.Add(newfile);
                await db.SaveChangesAsync();

                await Task.Factory.StartNew(() => InsertPdfTextToDbAsync(newfile.id, path));

                List <Category> categories = db.Categories.ToList();
                ViewBag.Categories = new SelectList(categories, "Id", "Name");
            }
            return(RedirectToAction("MyFiles", "File"));
        }