Esempio n. 1
0
        public async Task <IActionResult> UploadFileByte(UploadViewModel model, string returnUrl = null)
        {
            ViewData["ReturnUrl"] = returnUrl;
            List <IFormFile> files = new List <IFormFile>(model.FilePath);
            string           msg   = "";
            string           ret   = "";
            int    loc             = 0;
            string fileName        = "";

            if (ModelState.IsValid)
            {
                foreach (var formFile in files)
                {
                    if (formFile.Length > 0)
                    {
                        loc      = formFile.FileName.LastIndexOf('\\');
                        fileName = formFile.FileName.Substring(loc + 1);
                        using (var memoryStream = new MemoryStream()) {
                            await formFile.CopyToAsync(memoryStream);

                            var version = new DataBusiness.Version {
                                Thing       = 3,
                                CreateDate  = DateTime.Now,
                                DisplayName = model.DisplayName,
                                Name        = fileName,
                                FullPath    = formFile.FileName,
                                FileType    = formFile.ContentType,
                                Size        = (int)formFile.Length,
                                Desc        = model.Desc,
                                Comment     = model.Comment,
                                Item        = memoryStream.ToArray()
                            };
                            _context.Version.Add(version);
                            await _context.SaveChangesAsync();

                            msg = String.Format("File {0} from at {1} was successfully uploaded.", formFile.FileName, formFile.Name);
                            Serilog.Log.Information(msg);
                            ret += msg + Environment.NewLine;
                        }
                    }
                }
                // Don't rely on or trust the FileName property without validation.
                ViewData["Message"] = ret;
            }
            else
            {
                ViewData["Message"] = "Something was wrong with the submitted information. Please check input and try again.";
                Serilog.Log.Error(String.Format("Error uploading file for User {0}.", User.Identity.Name));                 //logging anything that might have been in msg
            }
            //return View("Index");
            return(RedirectToAction(nameof(Index)));
            //return RedirectToAction(nameof(UploadController.Index), "Upload");
        }
Esempio n. 2
0
 //[ValidateAntiForgeryToken]		//For some reason this breaks the download process - look into it when I have the time
 //[Route("[controller]/[action]")]
 public ActionResult DownloadFileByte(string id)
 {
     //return View("Index");
     //int fileID = 0;
     if (!int.TryParse(id, out int fileID))
     {
         ViewData["Message"] = "Invalid File Request. Please check you selection and try again. If this persists please contact Support.";
         Serilog.Log.Information("Invalid file version request. Version ID: {0}; User: {1};", id, User.Identity.Name);
         return(View("Index"));
     }
     DataBusiness.Version downloadVersion = (from version in _context.Version
                                             where version.Id == fileID
                                             select version).First();
     return(File(downloadVersion.Item, downloadVersion.FileType, downloadVersion.Name));
 }