1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162 |
- using Microsoft.AspNetCore.Hosting;
- using Microsoft.AspNetCore.Http;
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
-
- namespace Karsha_Site.Common
- {
- public class UploadFiles
- {
- private readonly IHostingEnvironment _environment;
- public UploadFiles(IHostingEnvironment hostingEnvironment)
- {
- _environment = hostingEnvironment;
- }
- public UploadDto UploadFile(IFormFile file, string folder)
- {
- if (file != null)
- {
- var uploadsRootFolder = Path.Combine(_environment.WebRootPath, folder);
- if (!Directory.Exists(uploadsRootFolder))
- {
- Directory.CreateDirectory(uploadsRootFolder);
- }
-
-
- if (file == null || file.Length == 0)
- {
- return new UploadDto()
- {
- Status = false,
- FileNameAddress = "",
- };
- }
-
- string fileName = DateTime.Now.Ticks.ToString() + file.FileName;
- var filePath = Path.Combine(uploadsRootFolder, fileName);
- using (var fileStream = new FileStream(filePath, FileMode.Create))
- {
- file.CopyTo(fileStream);
- }
-
- return new UploadDto()
- {
- FileNameAddress = folder + fileName,
- Status = true,
- };
- }
- return null;
- }
-
- public class UploadDto
- {
- public long Id { get; set; }
- public bool Status { get; set; }
- public string FileNameAddress { get; set; }
- }
- }
- }
|