Introduction
This is very basic level small user control, in which we can discuss how to create web user control.
To create user controls follow the steps.
- Right click on project
- Click on Add
- Select New Item
- Select Web User Control
- Specify some Name
I have given name to this file as "uploader.ascx" and kept this file under "userControl" for simplicity purpose.
On this pageI am having following controls.
-
<INPUT id="fileUpload" type="file" Runat="server" NAME="fileUpload"> -
<asp:label id="lblMessage" runat="server" Width="416px" Font-Size="10" Font-Name="verdana"></asp:label> -
<asp:button id="btnSave" runat="server" Text="Upload.."></asp:button>
At the code behind of above fileI am having following function.
public string UploadFile(string fileName, string folderName)
{
if (string.IsNullOrEmpty(fileName))
{
return "Invalid filename supplied";
}
if (fileUpload.PostedFile.ContentLength == 0)
{
return "Invalid file content";
}
fileName = System.IO.Path.GetFileName(fileName);
if (string.IsNullOrEmpty(folderName))
{
return "Path not found";
}
try
{
if (fileUpload.PostedFile.ContentLength <= 2048000)
{
fileUpload.PostedFile.SaveAs(Server.MapPath(folderName) + "\\" + fileName);
return "File uploaded successfully";
}
else
{
return "Unable to upload, file exceeds maximum limit";
}
}
catch (UnauthorizedAccessException ex)
{
return ex.Message + " Permission to upload file denied";
}
}
The above function takes care of following things before uploading file to the folder.
- Invalid file name supplied.
- If file not exists or content length 0.
- Folder name exists.
Error Handling in File Uploading
While uploading done with UnauthorizedAccessException and returned with the message.
On upload button click I am having following code.
private void btnSave_Click(object sender, EventArgs e)
{
string strFilename, strMessage;
strFilename = fileUpload.PostedFile.FileName.ToString();
strMessage = UploadFile(strFilename, ConfigurationSettings.AppSettings["folderPath"]);
lblMessage.Text = strMessage;
lblMessage.ForeColor = Color.Red;
}
I have made use of Web.config file, in whichI have added attribute as follows under.
<configuration> <appSettings> <add key="folderPath" value="Images"></add> </appSettings>
i.e.I have set uppath of folder to upload image.
To access control in project, I have added page called as "uploadTester.aspx" to the project in whichI have added following line.
<%@ Register TagPrefix="img" TagName="Uploader" src="userControl/uploader.ascx"%>
Which says that this control is register to this page with specified source.
And inHTML code I have added following code inside form tag
<img:Uploader runat="server" id="Uploader1"></img:Uploader>
That's all about.
How to Set permission to virtual directory
To upload any of the file in respective folder user need to have permission for writing to the folder so please follow the following steps to prevent from the error.
- Right Click on virtual directory which you have created for this project. Under directory Tab you will find
- Read
- Log Visits
- Index this resources Are marked as checked (enables)in addition to this make
- Write access enabled or checked
- Click on apply
- Click on ok
This will set right permission to entire virtual directory, this way we can minimize error from the front end for permission / access denied.
Other way to solve permission denied issue is to go to actual folder "images" by using physical path and follow these steps.
- Right click folder
- Sharing Tab
- Enable share this folder radio button
- Click Apply
- Click Ok
If u are using this code on 2000 server you should do following
- Right click respective folder
- Go to security tab
- Select Everyone user
- Apply full control
- click on ok
Kindly go through zip file for entire code.

Ganesh SonawanePosted Jan 11, 2017, 7:03 AM
Want to upload Zip file on server in UWP?? How can i acheive it Please help!!!!
M HKhanPosted Jan 26, 2014, 11:01 AM
Hi and how can we save the file in binary form in database
M HKhanPosted Jan 26, 2014, 10:45 AM
Hi how can we upload multiple files in one shot?
just matPosted Aug 12, 2011, 10:59 AM
Adding user id to the upload so that i can retrieve them using the userid
yogesh kolpePosted Jun 4, 2009, 6:05 AM
Hi............Munir sir.... Nice code......
siva prasadPosted Feb 7, 2008, 7:12 AM
Pls Help me. I have created in voice xml Recorded data. I want to Recorded data upload to IIS server folder with out upload control using c# aspx version 2.0.
Narendra Kumar GautamPosted Nov 23, 2007, 4:24 AM
Hello Sir I m Asking that can This artical work with asp.net 2.0 can u send me details on [email protected] As soon as possible Narendra
jobin thomaseditedPosted Aug 29, 2007, 6:39 AMEdited Aug 29, 2007, 6:41 AM
As u said i go through the error console in Mozilla and found the following error:'up_div has no properties'.How can i Solve this issue?
arshad mahmoodPosted Aug 8, 2007, 8:53 PM
Its working excelent. I really appreciate Munir's effort. Its coooool.
Prabhu KPosted Mar 22, 2007, 11:37 PM
Very Nice! Its work properly...I want to upload files on remote server.What can i do?Plz give the good suggestion.... Thanks in advance Prabhu.K
sunny desaiPosted Oct 6, 2006, 12:59 AM
I have successfully done file uploading.but I want a progressbar, which should be appear when the file is being uploaded and it should show the status of uploading. I am waiting for the reply. Thank you.
ARCHANA SINGHVIeditedPosted Aug 23, 2006, 2:32 PMEdited Nov 4, 2006, 4:39 AM
I want to upload the images to the images folder in my web application written in C#.I see ur code and wrote the same and include the user control in the desired Webform of the web application,made the Images folder also and set the Permission as desired and directed in the code. When i run the form ,i select the image but as soon as i click on the upload button, i got the ERROR of:- Object Reference Not Set To An Instance Of An Object on the line strFilename = fileUpload.PostedFile.FileName.ToString()In the private void btnSave_Click(object sender, System.EventArgs e) event. I have done my all efforts and include the Using System.Congiguration namespace also in my USerControl but All is vain. I am stuck for a month. Please Help me and give me Wright And Appropriate Code For File Uploading As soon as Possible. From:archana singhvi
Mohit YadavPosted Aug 21, 2006, 8:14 AM
Hi , The code given here is working ecellent.Kindly guide me how do I upload file on remote server. what should I write in web.config. Please send me the complete code as soon as possible. Thanks in advance Regards, Mohit
Victorino SamaritaPosted Jul 10, 2006, 8:42 PM
GREAT! I tried your uploader control and it works locally on my machine. Will it work when it is on public hosting? Do you have any personal web site?