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 page I 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 file I am having following function
public string uploadFile(string fileName,string folderName)
{
if(fileName=="")
{
return "Invalid filename supplied";
}
if(fileUpload.PostedFile.ContentLength==0)
{
return "Invalid file content";
}
fileName = System.IO.Path.GetFileName(fileName);
if(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";
}
}