Upload images with FTP in ASP.NET Core

Upload images with FTP by form in ASP.NET Core

Original YouTube Logo

Article have related YouTube video

Uploading images using FTP is one of the best and simplest ways to send images between your webapp/website to the ftp-server or web-server. In this article i will show you how you do it in ASP.NET Core.

02-02-2023 |

ASP.NET Core
Images
C#
FTP

So in this example i use er simple form for the cshtml file, where we send the image to our codebehind file by using the type 'file' in the input field.

You can go an put the form whereever you need to upload some images from.

The next part is a little bit more complicated, but don't worry it is not rocket science. There is 3 methods and a timestamp property in my example that you can include in your project.

First of all there is a OnPostAsync method that is being called when the form is submitted. I then store the image file in a variable, create a timestamp using the second method GetTimestamp.

Finally I call the Upload method that takes one parameter which is the image that have the data type IFormFile. In the Upload method I create a url string that will also create the file-name with a timestamp so you dont get errors if it already exists.

I then create a new FtpWebRequest instance that we can insert credentials and a method to called WebRequestMethods.Ftp.UploadFile.

At last I stream the file and copy it to the server.

Here you can see the whole index.cs file:

You can also go and download the FTP project here


Like what you see?


New C# Learning Area - Free!

Our website now offers a new section on C# theory, including code examples and related video tutorials.

ZetBit

Subscribe to ZetBits Newsletter