ASP.NET interview questions: – Various Ways to 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;
try
{
FileUpload1.PostedFile.SaveAs(location);
Label2.Text="The File has been successfully uploaded";
}
catch (Exception ex)
{
Label2.Text = "Error : " + ex.Message.ToString();
}
}
else
{
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: –


Get more learning materials for ASP.NET interview questions

Regards,

View author’s other article on  ASP.NET interview questions

Advertisements

About c# and .NET Interview questions

This blog is for developers who want to crack .NET and C# interviews. It has all tips and tricks needed to crack .NET interviews , C# interview , SQL Server interview , Java interview , WCF Interview , Silverlight interview , WPF interview , LINQ interview , Entity framework Interview. Do not forget to watch our Learn step by step video series. Learn MVC in 16 hours:- https://www.youtube.com/watch?v=Lp7nSImO5vk Learn AngularJS Step by Step:- https://www.youtube.com/watch?v=0kmdjqgO9IY Learn Design Pattern in 8 hours:- https://www.youtube.com/watch?v=YDobmucohqk Learn C# and .NET in 60 days:- https://www.youtube.com/watch?v=yh2SrzCkNQA Learn MSBI in 32 hours:- https://www.youtube.com/watch?v=mGPJx3ocFgg Learn SharePoint Step by Step in 8 hours:- https://youtu.be/C2fW76SwJNU
This entry was posted in Uncategorized and tagged , , , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s