Rename uploaded file to server

asp core 3 -- Posted on Oct. 30, 2020

rename uploaded file to server

              
                public async Task AddFile(Object, IFormFile File)
        {
            if (File != null && File.Length > 0)
            {
                var guid = Guid.NewGuid().ToString();
                guid = guid.Substring(guid.Length - 4);
                string filename = Path.GetFileNameWithoutExtension(File.FileName) + "__" + guid;
                var extension = Path.GetExtension(File.FileName).ToLower();
                var name = $"{filename}{extension}";
                var filePath = Path.Combine(Directory.GetCurrentDirectory(), @"wwwroot\folder", name);

                // Create new local file and copy contents of uploaded file
                //using (var localFile = System.IO.File.OpenWrite(filePath))
                using (var uploadedFile = new FileStream(filePath, FileMode.Create))
                {
                    await File.CopyToAsync(uploadedFile);
                }
                object.Filepath = name;
            }
        }
                  
   
            

Related Posts