コード例 #1
0
        public async Task <IActionResult> FileUpload(IFormFile file, [Bind("id,name,text")] DocumentsIB documentsIB)
        {
            if (file != null && file.Length > 0)
            {
                var filePath   = @"\Upload\Files";
                var uploadPath = _env.WebRootPath + filePath;

                //Create
                if (!Directory.Exists(uploadPath))
                {
                    Directory.CreateDirectory(uploadPath);
                }

                //Create Uniq file name
                var    uniqFileName = Guid.NewGuid().ToString();
                var    filename     = Path.GetFileName(uniqFileName + "." + file.FileName.Split(".")[1].ToLower());
                string fullPath     = uploadPath + filename;

                filePath = filePath + @"\";
                var filesPath = @".." + Path.Combine(filePath, filename);
                documentsIB.name = file.FileName;
                documentsIB.text = Url.Content(fullPath);
                _context.DocumentsIB.Add(documentsIB);
                _context.SaveChanges();
                using (var fileStream = new FileStream(fullPath, FileMode.Create))
                {
                    await file.CopyToAsync(fileStream);
                }


                ViewData["FileLocation"] = filePath;
            }
            return(View("../DocumentsIBs/Index", await _context.DocumentsIB.ToListAsync()));
        }