In this article you will learn how to upload file in asp.net using c#
<asp:Button runat="server" id="UploadButton" text="Upload" OnClick="UploadButton_Click" />
<asp:Label runat="server" id="StatusLabel" text="Upload status: " />
</div>
Step 4: In your aspx.cs file you have to type following code.
protected void UploadButton_Click(object sender, EventArgs e)
{
if (FileUploadControl.HasFile)
{
int size = FileUploadControl.PostedFile.ContentLength;
if(size>2097152)
{
StatusLabel.Text = "Upload status: The file could not be uploaded. The size should be less the 2 Mb ";
}
else
{
if (System.IO.Path.GetExtension(FileUploadControl.FileName).ToLower() != ".doc"
&& System.IO.Path.GetExtension(FileUploadControl.FileName).ToLower() != ".docx"
&& System.IO.Path.GetExtension(FileUploadControl.FileName).ToLower() != ".pdf")
{
StatusLabel.Text = "Upload status: The file could not be uploaded. The file should be of type doc,docx or pdf ";
}
else
{
try
{
string filename = System.IO.Path.GetFileName(FileUploadControl.FileName);
FileUploadControl.SaveAs(Server.MapPath("~/employee_cv/") + filename);
StatusLabel.Text = "Upload status: File uploaded!";
}
catch (Exception ex)
{
StatusLabel.Text = "Upload status: The file could not be uploaded. The following error occured: " + ex.Message;
}
}
}
}
}
What this code will do . This will first check id the fileuploadcontrol has a file or not and then it will check whether the size of the file is less the 2 Mb or not (i.e 2097152 Bytes ) After that it will check the extension of the file whether it is doc,docx or a pdf file or not. If if all goes well te it will upload that file in the employee_cv folder that i have created in my project. you can change the ("~/employee_cv/") to your folder name like ("/your_folder_name/") and that's it.
Step 5: Run your Application in the browser and check whether it will work or not.
If you find any kind of problem then feel free to contact me or comment here. I will try to solve your problem as soon as possible.
Thanks for your time.
fileupload control in asp.net
Step 1: Create an Asp.net project in visual Studio
Step 2: Add a Web form in your project by right clicking on your project name and the Add>Web Form
Step 3: Name your web form whatever you like and paste the following code inside the aspx file of your web form.
<div>
<asp:FileUpload id="FileUploadControl" runat="server" /><asp:Button runat="server" id="UploadButton" text="Upload" OnClick="UploadButton_Click" />
<asp:Label runat="server" id="StatusLabel" text="Upload status: " />
</div>
Step 4: In your aspx.cs file you have to type following code.
protected void UploadButton_Click(object sender, EventArgs e)
{
if (FileUploadControl.HasFile)
{
int size = FileUploadControl.PostedFile.ContentLength;
if(size>2097152)
{
StatusLabel.Text = "Upload status: The file could not be uploaded. The size should be less the 2 Mb ";
}
else
{
if (System.IO.Path.GetExtension(FileUploadControl.FileName).ToLower() != ".doc"
&& System.IO.Path.GetExtension(FileUploadControl.FileName).ToLower() != ".docx"
&& System.IO.Path.GetExtension(FileUploadControl.FileName).ToLower() != ".pdf")
{
StatusLabel.Text = "Upload status: The file could not be uploaded. The file should be of type doc,docx or pdf ";
}
else
{
try
{
string filename = System.IO.Path.GetFileName(FileUploadControl.FileName);
FileUploadControl.SaveAs(Server.MapPath("~/employee_cv/") + filename);
StatusLabel.Text = "Upload status: File uploaded!";
}
catch (Exception ex)
{
StatusLabel.Text = "Upload status: The file could not be uploaded. The following error occured: " + ex.Message;
}
}
}
}
}
What this code will do . This will first check id the fileuploadcontrol has a file or not and then it will check whether the size of the file is less the 2 Mb or not (i.e 2097152 Bytes ) After that it will check the extension of the file whether it is doc,docx or a pdf file or not. If if all goes well te it will upload that file in the employee_cv folder that i have created in my project. you can change the ("~/employee_cv/") to your folder name like ("/your_folder_name/") and that's it.
Step 5: Run your Application in the browser and check whether it will work or not.
If you find any kind of problem then feel free to contact me or comment here. I will try to solve your problem as soon as possible.
Thanks for your time.
ConversionConversion EmoticonEmoticon