How to save IFormFile to Disk or File path

Today in this article, we shall see how to save IFormFile to disk or file path in the .NET API project.

Today in this article, we will cover below aspects,

We shall be using IFormFile and then use it to save streams of bytes to the disk file path locally.

Please note that if you are using IFormFile Interface in the API for sending a file sent with the HttpRequest, then it is pretty straightforward to use it in the API and save it back to a file, etc. using a stream of the file object.

How to use IFormFile

The sample API method is as below,

        [HttpPost]
        public Task Upload(IList<IFormFile> files)
        {
            return Task.CompletedTask;
        }

Above we are using the IFormFile interface which represents a file sent with the HttpRequest.

Save a Stream to a File using C#

In the above code, the CopyTo method Copies the contents of the uploaded file to the target stream.

If using Asynchronous API then please use the CopyToAsync method which helps in Asynchronously copying the contents of the uploaded file to the target stream without blocking the main thread.

If you are using a non-host C# application, you can very much use the same above approach to save a Stream to a File.

Verifying the IFormFile using Swagger

Let’s now test the above logic using Swagger or Postman,

how do i save a stream to a file in csharp

Once the API gets a request, the file will be saved locally to the disk path successfully.

For simplicity, we will write the logic within the Controller method.

How to save IFormFile to Disk or File path, iformfile example, iformfile typescript, create iformfile, iformfile openreadstream, iformfile in c#, iformfile angular, javascript iformfile, iformfile extension,

Here is the complete code,

        [HttpPost]
        public Task Download(IList<IFormFile> files)
        {
            string uploads = Path.Combine(_hostingEnvironment.ContentRootPath, "Uploads");
            //Create directory if it doesn't exist 
            Directory.CreateDirectory(uploads);
            foreach (IFormFile file in files)
            {
                if (file.Length > 0)
                {
                    string filePath = Path.Combine(uploads, file.FileName);
                    using (Stream fileStream = new FileStream(filePath, FileMode.Create, FileAccess.Write))
                    {
                        file.CopyTo(fileStream);
                    }
                }
            }
            return Task.CompletedTask;
        }

You can very much upload .csv or .xlsx files easily using the same above logic and save them as files using the target format.

In the above codes, files will be downloaded successfully to the application root path i.e _hostingEnvironment.ContentRootPath.

Do you have any comments or ideas or any better suggestions to share?

Please sound off your comments below.

Happy Coding !!



Please bookmark this page and share it with your friends. Please Subscribe to the blog to receive notifications on freshly published(2024) best practices and guidelines for software design and development.



Leave a Reply

Your email address will not be published. Required fields are marked *