ASP.NET interview questions: - Describe how can we Browse and Upload a file in ASP.NET?
This not one of those most asked ASP.NET questions , but yes sometimes interviewer starts getting in to practical things to understand how good you are as a developer.
So let's see a small and simple example to understand it in better manner.
In order to see it practically just follow the following steps.
Step1: - create a new project of ASP.NET for that just go to >> File >> New >> Project >> Web >> select ASP.NET Empty Web Application.
Step2: - now, just simply add a Web form page in to your application for that just go to >> Solution Explorer >> Right click on the project name >> Add >> New Item >> Select Web Form.
Step3: - Now Add a FileUpload control and Button control from the ToolBox to the WebForm.aspx Page.
The WebForm.aspx page should appear like below diagram.
Step4: - Now simply just add the below code snippet in to WebForm1.aspx.cs file.
protected void Button1_Click(object sender, EventArgs e)
if ((FileUpload1.PostedFile != null) && (FileUpload1.PostedFile.ContentLength > 0))
string filename = System.IO.Path.GetFileName(FileUpload1.PostedFile.FileName);
string location = Server.MapPath("")+"\\"+filename;
Label2.Text="The File has been successfully uploaded";
catch (Exception ex)
Label2.Text = "Error : " + ex.Message.ToString();
Label2.Text = "Please Enter a File";
The above code snippet is for the Button control for uploading the selected file to the server.
Once you have completed all the above steps just run your application to get a resultant result.
The above diagram is the running page we have created, now just click on the browse button to select a file from desired location.
Once you have selected the file now just click on the upload button.
Now if everything goes right you will see a result like below diagram.
View the following video on use of Static keyword: -