0

I am using ImageSharp library in my ASP.NET Core Web API project.

I want to upload an image after resizing by ImageSharp to ftp server.

I am using fluentftp for uploading image.this is my code below

public async Task<string> UploadImage(IFormFile file)
{
    string newFileName = "";

    if (file != null)
    {
        if (file.Length > 0)
        {
            var fileName = Path.GetFileName(file.FileName);
            var guid = Convert.ToString(Guid.NewGuid().ToString("N"));
            var now = DateTime.Now.TimeOfDay.Ticks;
            var myUniqueFileName = $"{guid}_{now}";
            var fileExtension = Path.GetExtension(fileName);
            newFileName = String.Concat(myUniqueFileName, fileExtension);
            var filepath = new PhysicalFileProvider(Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "images")).Root + $@"\{newFileName}";

            using (var image = Image.Load(file.OpenReadStream()))
            {
                string newsize = ImageResize(image, 800, 600);
                string[] sizearray = newsize.Split(',');

                image.Mutate(x => x.Resize(Convert.ToInt32(sizearray[1]), Convert.ToInt32(sizearray[0])));

                var client = new AsyncFtpClient("server", "username", "password");
                await client.AutoConnect();
                await client.UploadFile(@"C:\MyVideo.mp4", "/htdocs/MyVideo.mp4");
                await client.Disconnect();

                image.Save(filepath);
            }
        }
        else 
            return null!;

        return newFileName;
    }
    else 
        return null!;
}

Any help will be highly appreciated.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459

0 Answers0