Suggested Videos
Part 19 - Mapping virtual path to physical path using Server.MapPath method
Part 20 - Mapping virtual path to physical path using Server.MapPath method Example
In this video we will discuss about fileupload control. FileUpload control is a combination of a text box and a browse button that enable users to select a file to upload to the server.
Create an asp.net web application project. Drag and drop the FileUpload control on the webform.
The fileUpload control only allows the user to select the file. To upload the seleceted file, drag and drop a button control. Change the ID of the button to btnUpload and the Text to Upload File. Also drag and drop a label control, and change the ID of the label to lblMessage. At this stage the HTML of the webform should be as shown below.
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Button ID="btnUpload" runat="server" Text="Upload File"
onclick="btnUpload_Click" />
<br />
<asp:Label ID="lblMessage" Font-Bold="true" runat="server">
</asp:Label>
Right click on the web application project and add a folder with name Uploads. This folder is going to store all the uploaded files.
Copy and paste the following code in btnUpload_Click() event handler
// If the user has selected a file
if (FileUpload1.HasFile)
{
// Get the file extension
string fileExtension = System.IO.Path.GetExtension(FileUpload1.FileName);
if (fileExtension.ToLower() != ".doc" && fileExtension.ToUpper() != ".docx")
{
lblMessage.ForeColor = System.Drawing.Color.Red;
lblMessage.Text = "Only files with .doc and .docx extension are allowed";
}
else
{
// Get the file size
int fileSize = FileUpload1.PostedFile.ContentLength;
// If file size is greater than 2 MB
if (fileSize > 2097152)
{
lblMessage.ForeColor = System.Drawing.Color.Red;
lblMessage.Text = "File size cannot be greater than 2 MB";
}
else
{
// Upload the file
FileUpload1.SaveAs(Server.MapPath("~/Uploads/" + FileUpload1.FileName));
lblMessage.ForeColor = System.Drawing.Color.Green;
lblMessage.Text = "File uploaded successfully";
}
}
}
else
{
lblMessage.ForeColor = System.Drawing.Color.Red;
lblMessage.Text = "Please select a file";
}
Part 19 - Mapping virtual path to physical path using Server.MapPath method
Part 20 - Mapping virtual path to physical path using Server.MapPath method Example
In this video we will discuss about fileupload control. FileUpload control is a combination of a text box and a browse button that enable users to select a file to upload to the server.
Create an asp.net web application project. Drag and drop the FileUpload control on the webform.
The fileUpload control only allows the user to select the file. To upload the seleceted file, drag and drop a button control. Change the ID of the button to btnUpload and the Text to Upload File. Also drag and drop a label control, and change the ID of the label to lblMessage. At this stage the HTML of the webform should be as shown below.
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Button ID="btnUpload" runat="server" Text="Upload File"
onclick="btnUpload_Click" />
<br />
<asp:Label ID="lblMessage" Font-Bold="true" runat="server">
</asp:Label>
Right click on the web application project and add a folder with name Uploads. This folder is going to store all the uploaded files.
Copy and paste the following code in btnUpload_Click() event handler
// If the user has selected a file
if (FileUpload1.HasFile)
{
// Get the file extension
string fileExtension = System.IO.Path.GetExtension(FileUpload1.FileName);
if (fileExtension.ToLower() != ".doc" && fileExtension.ToUpper() != ".docx")
{
lblMessage.ForeColor = System.Drawing.Color.Red;
lblMessage.Text = "Only files with .doc and .docx extension are allowed";
}
else
{
// Get the file size
int fileSize = FileUpload1.PostedFile.ContentLength;
// If file size is greater than 2 MB
if (fileSize > 2097152)
{
lblMessage.ForeColor = System.Drawing.Color.Red;
lblMessage.Text = "File size cannot be greater than 2 MB";
}
else
{
// Upload the file
FileUpload1.SaveAs(Server.MapPath("~/Uploads/" + FileUpload1.FileName));
lblMessage.ForeColor = System.Drawing.Color.Green;
lblMessage.Text = "File uploaded successfully";
}
}
}
else
{
lblMessage.ForeColor = System.Drawing.Color.Red;
lblMessage.Text = "Please select a file";
}
Hello Venkat,
ReplyDeleteThanks for the tutorials. How can the code be modified so as to save the path of the file uploaded to a database?
Thanks for the tutorials. How can the code be modified so as to save the path of the file uploaded to a database?
ReplyDeletehow to rename image with id.plz its urgent
ReplyDeleteI think you want to write
ReplyDeleteif (fileExtension.ToLower() != ".doc" && fileExtension.ToLower() != ".docx")
Instead of
if (fileExtension.ToLower() != ".doc" && fileExtension.ToUpper() != ".docx")
.docx is next version of doc .
Delete@Tiago
DeleteYou Right need to convert it to lower not upper
or use upper and write it like ".DOCX"
sir how can i overrite the existing file while uploading a new file
ReplyDeleteHello sir,
ReplyDeletehow can i upload image in running mode on a web page.
Your all tutorials are really excellent.
LOVE your tutorials! I have one question, I'd like to change the upload size to 6 MB, so I changed it to below, but instead of getting the nice red error I get an ugly exception. Ideas?
ReplyDelete// Get the file size
int fileSize = FileUpload1.PostedFile.ContentLength;
// If file size is greater than 6 MB
if (fileSize > 6291456)
{
lblMessage.ForeColor = System.Drawing.Color.Red;
lblMessage.Text = "File size cannot be greater than 6 MB";
}
else
{
// Upload the file
FileUpload1.SaveAs(Server.MapPath("~/Uploads/" + FileUpload1.FileName));
lblMessage.ForeColor = System.Drawing.Color.Green;
lblMessage.Text = "File uploaded successfully";
}
if (fileExtension.ToLower() != ".doc" && fileExtension.ToUpper() != ".docx")
ReplyDeletehere ToUpper has be changed to ToLower or .docx to .DOCX