Nlabs.FileSrevice 8.0.5

Suggested Alternatives

Nlabs.FileService 8.0.7

Additional Details

Package name corrected.

dotnet add package Nlabs.FileSrevice --version 8.0.5
NuGet\Install-Package Nlabs.FileSrevice -Version 8.0.5
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Nlabs.FileSrevice" Version="8.0.5" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Nlabs.FileSrevice --version 8.0.5
#r "nuget: Nlabs.FileSrevice, 8.0.5"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Nlabs.FileSrevice as a Cake Addin
#addin nuget:?package=Nlabs.FileSrevice&version=8.0.5

// Install Nlabs.FileSrevice as a Cake Tool
#tool nuget:?package=Nlabs.FileSrevice&version=8.0.5

Dependency

This library was created by .Net 8.0

Install

dotnet add package Nlabs.FileService

Usage

If you want to save file in server


IoC Configuration and Usage of FileService
This WebAPI project manages static files (wwwroot directory) using the Nlabs.FileService package. To utilize this package, you need to perform dependency injection (DI) and provide the necessary configuration.

Step 1: Adding Dependencies to IoC Container
In your Program.cs file, use the AddFileService method to configure the IFileHostEnvironment interface. This method ensures that the necessary dependencies are registered correctly in the IoC container:

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
builder.Services.AddFileService(builder.Environment.WebRootPath);



string filePath = "./Files/";


string fileName = FileService.FileSaveToServer(file,filePath);

If you want to save file in Ftp

FileSaveToFtpModel fileSaveToFtpModel = new("ftp.ftpadresiniz.com","userName", "password");

string fileName = FileService.FileSaveToFtp(file,fileSaveToFtpModel);

If you want to save file in Database (byte[])

byte[] fileByeArray = FileService.FileConvertByteArrayToDatabase(file);

If you want to delete file in server

//string path = "./Files/" + "FileName";

If you want to use the package using layered architecture
Example Handler Code:
internal sealed class MyCommandHandler(IFileHostEnvironment fileHostEnvironment) : IRequestHandler
bla bla bla
this handle code

var fullPath = Path.Combine(fileHostEnvironment.WebRootPath, "Files", file.jpeg);

FileService.FileDeleteToServer(path);

If you want to delete file in ftp

FileSaveToFtpModel fileSaveToFtpModel = new("ftp.ftpadresiniz.com","userName", "password");
string path = "./Files/" + "FileName";

FileService.FileDeleteToFtp(path,fileSaveToFtpModel);

Method and Class

public class FileSaveToFtpModel
    {
        public FileSaveToFtpModel(string ftpAddress, string userName, string password)
        {
            FtpAddress = ftpAddress;
            UserName = userName;
            Password = password;
        }
        public string FtpAddress { get; set; }
        public string UserName { get; set; }
        public string Password { get; set; }
    }
public static class FileService
    {
        public static string FileSaveToServer(IFormFile file, string filePath)
        {
            var fileFormat = file.FileName.Substring(file.FileName.LastIndexOf('.'));
            fileFormat = fileFormat.ToLower();
            string fileName = Guid.NewGuid().ToString() + fileFormat;
            string path = filePath + fileName;
            using (var stream = System.IO.File.Create(path))
            {
                file.CopyTo(stream);
            }
            return fileName;
        }

        public static string FileSaveToFtp(IFormFile file, FileSaveToFtpModel fileSaveToFtpModel)
        {
            var fileFormat = file.FileName.Substring(file.FileName.LastIndexOf('.'));
            fileFormat = fileFormat.ToLower();
            string fileName = Guid.NewGuid().ToString() + fileFormat;

            FtpWebRequest request = (FtpWebRequest)WebRequest.Create(
                $"{fileSaveToFtpModel.FtpAddress}{fileName}");
            request.Credentials = new NetworkCredential(
                fileSaveToFtpModel.UserName,
                fileSaveToFtpModel.Password);
            request.Method = WebRequestMethods.Ftp.UploadFile;

            using (Stream ftpStream = request.GetRequestStream())
            {
                file.CopyTo(ftpStream);
            }

            return fileName;
        }

        public static byte[] FileConvertByteArrayToDatabase(IFormFile file)
        {
            using (var memoryStream = new MemoryStream())
            {
                file.CopyTo(memoryStream);
                var fileBytes = memoryStream.ToArray();
                string fileString = Convert.ToBase64String(fileBytes);
                return fileBytes;
            }
        }

        public static void FileDeleteToServer(string path)
        {
            try
            {
                if (System.IO.File.Exists(path))
                {
                    System.IO.File.Delete(path);
                }
            }
            catch (Exception)
            {
            }
        }

        public static void FileDeleteToFtp(string path, FileSaveToFtpModel fileSaveToFtpModel)
        {
            try
            {
                FtpWebRequest request = (FtpWebRequest)WebRequest.Create(
                    $"{fileSaveToFtpModel}{path}");
                request.Credentials = new NetworkCredential(
                    fileSaveToFtpModel.UserName,
                    fileSaveToFtpModel.Password);
                request.Method = WebRequestMethods.Ftp.DeleteFile;
                FtpWebResponse response = (FtpWebResponse)request.GetResponse();
            }
            catch (Exception)
            {
            }
        }
    }
Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
8.0.5 46 6/27/2024
8.0.4 30 6/27/2024
8.0.3 31 6/27/2024
8.0.2 31 6/26/2024
8.0.1 32 6/26/2024
1.0.0 73 6/15/2024