Tuesday, February 15, 2011

File Upload User Control

What it does?
The utility is a small user control that has the following in the ascx file

a. asp Label for texts before the file upload
b. asp file upload
c. asp Button
d. asp Label for Status.

In the ascx.cs file
There are 3 properties
a. setLabelText/getLabelText
b. setButtonText/getButtonText
c. setUploadPath/getUploadPath
and a delegate to pass the file full path with file name to the aspx page that is calling the User Control.
In the Page Load, the Text of the Labels are set that are passed by the calling page.
In the Button Click Event, the file is uploaded and the status of the Error Label is set and finally we are setting the event with the string filename.

In the aspx page, in Page Load we are setting the properties, calling the delegate and just writing the file path in the screen, this can be used to pass the information to a SP for Bulk Upload of the Excel File in database. (in another post )



Put the following code in ascx file

<div>
<span>
<asp:label id="lblUpload" runat="server"></asp:label>: <asp:fileupload id="txtUpload" runat="server">
</asp:fileupload></span>
<span><asp:button runat="server" id="btnUpload" onclick="btnUpload_Click"></asp:button></span>
<span> <asp:label id="lblError" runat="server"></asp:label></span>
</div>


Put the following in .ascx.cs file
public delegate void SendMsgToPage(string MessageToPage);

public partial class UploadFile : System.Web.UI.UserControl
{
public event SendMsgToPage sndMsgToPage;
private string _setLabelText, _setButtonText, _fileUploadPath, _file, _dbData;
bool _WriteToDb;

public void setLabelText(string Text)
{
_setLabelText = Text;
}
public string getLabelText()
{
if (_setLabelText == null || _setLabelText == "")
{
_setLabelText = "Select File";
}
return _setLabelText;
}
public void setButtonText(string Text)
{
_setButtonText = Text;
}
public string getButtonText()
{
if (_setButtonText == null || _setButtonText == "")
{
_setButtonText = "Upload";
}
return _setButtonText;
}

public void setUploadPath(string Text)
{
_fileUploadPath = Text;
}
public string getUploadPath()
{
if (_fileUploadPath == null || _fileUploadPath == "")
{
_fileUploadPath = "Uploads";
}
return _fileUploadPath;
}

protected void Page_Load(object sender, EventArgs e)
{
lblUpload.Text = _setLabelText;
btnUpload.Text = _setButtonText;
lblError.Visible = false;
}

protected void btnUpload_Click(object sender, EventArgs e)
{
lblError.Visible = true;
try
{
_file = Server.MapPath(_fileUploadPath) + "\\" + txtUpload.FileName;
txtUpload.SaveAs(_file);
lblError.Text = "File: " + txtUpload.FileName + " Uploaded Successfully";
}
catch (Exception ex)
{
_file = "";
lblError.Text = "File: " + txtUpload.FileName + " couldnot be uploaded due to " + ex.Message;
}
finally
{
sndMsgToPage(_file);
}
}
}

Put the following in the aspx file calling the User Control

<%@ Register src="Controls/UploadFile.ascx" tagname="UploadFile" tagprefix="uc1" %>

<uc1:uploadfile id="fileUpd" runat="server">
</uc1:uploadfile>


in the aspx.cs file
fileUpd.setLabelText(Put Label Text);
fileUpd.setButtonText(Put Button Text);
fileUpd.setUploadPath(Set the Upload Path); //should be a folder inside the current project
fileUpd.sndMsgToPage += delegate(string fileUploadPath)
{
Response.Write(fileUploadPath);
};

No comments: