Upload File and Delete File in ASP.NET Core – Session 52

upload file and delete file in ASP.NET Core MVC - Session 52

During this session, we will discuss Upload File in ASP.NET Core and Delete File in ASP.NET Core and implement these features on our Web App. Also, we use the GUID class to assign a unique file name for the uploaded file. Then, we are going to store the file name inside the respective field for further use.

GUID stands for Globally Unique Identifier and is a 128 bits string that assigns as Identification. Developers use this feature for IDs on computers, networks, and Databases. GUID string looks like a serial number and is used for preventing duplication.

What is GUID?

GUID stands for Globally Unique Identifier and is a 128 bits string that assigns as Identification. Developers use this feature for IDs on computers, networks, and Databases.

Upload File in ASP.NET Core

To upload file(s) in ASP.NET Core we need to pass the file from View to Controller. Thus, we need to add the IFormFile property to the respective Model. Moreover, we need to set the enctype attribute of the form tag to multipart/form-data. Later, we should add the respective input element to the form which should be bound to the IFormFile property with aspfor tag helper.

For better understanding, we implement the mentioned changes on our Web App and start with adding the IFormFile property to CreateCostViewModel.

Next, we implement the required changes in Create View.

Now, we need to do some changes to the respective Controller. First, to have access to the path of the Web App, we can inject an object of IWebHostEnvironment through the Controller Constructor. Next, we need to check whether posted data has a file or not. If there is a file, we should assign a unique file name to the uploaded file via GUID class. Moreover, we need to find the destination of the file through the object of IWebHostEnvironment. After that, we make a FileStream and copy the file to the destination with the CopyTo method of the IFormFile object.

To apply the above changes to our Web App, first, we inject IWebHostEnvironment into the CostController.

Next, to obey the Don’t Repeat Yourself principle, we create a Non-Action Method for the upload process. This method handles assigning unique file name processes as well.

Finally, inside Create Action Method with HttpPost decoration, we check the existence of the file and call the UploadFile method in case of file existence. Also, we store the unique file name inside the InvoiceImagePath property of the Cost entity.

Delete File in ASP.NET Core

To delete an uploaded file, just we need to find its path. Then we can manage to delete files with System.IO.File.Delete method.

Here, we need to implement the Delete File process for the Update and Delete Action Method of CostController. Hence, for the Update operation, we need to create a ViewModel that contains the IFormFile property. As most of the properties are the same between CreateCostViewModel and the ViewModel which we will create for Update purposes, the new ViewModel can be implemented from CreateCostViewModel.

Then, we should modify Update View to display the possible record image and input element to update the file of the record.

As you can see in the above code, we create an object to have access to an image of the record to be displayed during the update process. Also, we implement the same technique for the Detail view to display the image of the selected record. you can review the Detail view code below.

As we changed the Model of the Update View, we should do some changes inside Update Action Method with HttpGet decoration.

Then we need to check whether posted data to the Update Action Method contains a file or not. If there is a file, we need to replace it with the possible former image. Thus, we check if the record has uploaded an image, first we delete the previous image and then upload the new one. Also, if the record does not have a previous image, we just need to upload the current image.

The last part of this session is implementing the Delete process in Delete Action Method. Hence, we check the record that is posted to the Delete Action method, and if there is a file, we delete the file first and then delete the record from the Database.

If you need more details, watch this session video. Also, for being updated about our coming sessions, follow us on Instagram, Facebook, Telegram, or YouTube. Moreover, you can have access to the list of all sessions HERE and you can download this session source code from our GitHub.

You can download this Session Slides form HERE.

0.00 avg. rating (0% score) - 0 votes

Tags

3 3 votes
Article Rating
guest

0 Comments
Inline Feedbacks
View all comments