Exemplo n.º 1
0
        public IActionResult UploadFiles(IFormFile files)
        {
            if (files == null)
            {
                Console.WriteLine("No file found");
                return(NotFound());
            }

            long size = files.Length;

            Console.WriteLine("\r\n!!!!New file {0}, files size: {1}\r\n", files.FileName, size);


            string path = "./wwwroot/Files/";
            string id   = GetToken();

            ApplicationContext.UsersData.TryAdd(id, new UserData());
            string newPath = path + id + "/";

            Directory.CreateDirectory(path + id);
            using (var ms = new MemoryStream())
            {
                ms.Position = 0;
                files.CopyTo(ms);
                if (!CheckFileFormat(files.FileName))
                {
                    return(BadRequest(new { Message = "Bad File " + files.FileName }));
                }

                if (!CheckFileStruct(ms))
                {
                    return(BadRequest(new { Message = "Bad File " + files.FileName }));
                }
                ms.Position = 0;
                ApplicationContext.UsersData[id].inputFile.Position = 0;
                ms.CopyTo(ApplicationContext.UsersData[id].inputFile);
            }
            BackgroundWorker worker = new BackgroundWorker();

            worker.DoWork += (object sender, DoWorkEventArgs arg) =>
            {
                ReviewFieldController.Build(id);
            };
            worker.RunWorkerCompleted +=
                (object sender, RunWorkerCompletedEventArgs arg) =>
            {
                ApplicationContext.UsersData[id].isReady = true;
                Console.WriteLine("\r\nFile {0} builded \r\n", id);
            };
            worker.RunWorkerAsync();
            //_context.SaveChanges();

            return(Ok(new { Id = id }));
        }
        public IActionResult UploadFiles(IFormFile files)
        {
            if (files == null)
            {
                Console.WriteLine("{0}\tПопытка загрузить пустой файл", DateTime.Now);
                return(BadRequest());
            }

            long size = files.Length;

            if (size > 102400)
            {
                Console.WriteLine("{0}\tПопытка загрузить слишком большой файл", DateTime.Now);
                return(BadRequest(new { Error = "Файл слишком большой" }));
            }
            string id = GetToken();

            ApplicationContext.UsersData.TryAdd(id, new UserData());
            using (var ms = new MemoryStream())
            {
                ms.Position = 0;
                files.CopyTo(ms);
                if (!CheckFileFormat(files.FileName))
                {
                    return(BadRequest(new { Message = "Bad File " + files.FileName }));
                }
                if (!CheckFileStruct(ms))
                {
                    return(BadRequest(new { Message = "Bad File " + files.FileName }));
                }
                Console.WriteLine("{0}\tNew file {1}, files size: {2}",
                                  DateTime.Now, files.FileName, size);
                ms.Position = 0;
                ApplicationContext.UsersData[id].inputFile.Position = 0;
                ms.CopyTo(ApplicationContext.UsersData[id].inputFile);
            }
            ApplicationContext.UsersData[id].uploadFile = DateTime.Now;
            BackgroundWorker worker = new BackgroundWorker();

            worker.DoWork += (object sender, DoWorkEventArgs arg) =>
            {
                Console.WriteLine("{0}\tStart building file {1}", DateTime.Now, id);
                ReviewFieldController.Build(id);
            };
            worker.RunWorkerCompleted +=
                (object sender, RunWorkerCompletedEventArgs arg) =>
            {
                ApplicationContext.UsersData[id].isReady = true;
                Console.WriteLine("{0}\tFile {1} builded", DateTime.Now, id);
            };
            worker.RunWorkerAsync();

            return(Ok(new { Id = id }));
        }