Uploading a PDF or Word File having Limited file Size.
Step 1 : Add File Upload tool from the toolbox.
Step 2: On Button click event
Calling the function on button click to validate the file uploaded is of the format mentioned and has size below the limit defined. Here the file Size is Maximum of 1 MB.
Following are the code validate it:
protected void btnSubmit_Click(object sender, EventArgs e)
{
try
{
string file = "";
if (FileUpload1.HasFile)
{
file = UploadFile();
}
}
catch (Exception ex)
{
DisplayAJAXMessage(this, "Error::" + ex);
}
}
------------------------------------------------------------------------
private string UploadFile()
{
string file = lblGLId.Text + "_" + FileUpload1.FileName;
if (FileUpload1.PostedFile.ContentLength < 1000000)
{
string Extension = Path.GetExtension(FileUpload1.PostedFile.FileName);
if ((Convert.ToString(Extension).ToLower() == ".pdf") ||
(Convert.ToString(Extension).ToLower() == ".doc") ||
(Convert.ToString(Extension).ToLower() == ".docx"))
{
string path = Server.MapPath(".") + "\\FolderName\\" + file;
FileUpload1.PostedFile.SaveAs(path);
}
else
{
DisplayAJAXMessage(this, "Only file with extension .PDF/.DOC/.DOCX Allowed to be Uploaded");
}
}
else
{
DisplayAJAXMessage(this, "Maximum file size upto 1MB");
}
return file;
}
---------------------------------------------------------------------------------
static public void DisplayAJAXMessage(Control page, string msg)
{
string myScript = String.Format("alert('{0}');", msg);
ScriptManager.RegisterStartupScript(page, page.GetType(), "MyScript", myScript, true);
}
Note : The "FolderName" should be a folder where the uploaded file is to be kept in project.
Step 1 : Add File Upload tool from the toolbox.
![]() |
Fig. 1 : To Add upload tool from toolbox |
Calling the function on button click to validate the file uploaded is of the format mentioned and has size below the limit defined. Here the file Size is Maximum of 1 MB.
Following are the code validate it:
protected void btnSubmit_Click(object sender, EventArgs e)
{
try
{
string file = "";
if (FileUpload1.HasFile)
{
file = UploadFile();
}
}
catch (Exception ex)
{
DisplayAJAXMessage(this, "Error::" + ex);
}
}
------------------------------------------------------------------------
private string UploadFile()
{
string file = lblGLId.Text + "_" + FileUpload1.FileName;
if (FileUpload1.PostedFile.ContentLength < 1000000)
{
string Extension = Path.GetExtension(FileUpload1.PostedFile.FileName);
if ((Convert.ToString(Extension).ToLower() == ".pdf") ||
(Convert.ToString(Extension).ToLower() == ".doc") ||
(Convert.ToString(Extension).ToLower() == ".docx"))
{
string path = Server.MapPath(".") + "\\FolderName\\" + file;
FileUpload1.PostedFile.SaveAs(path);
}
else
{
DisplayAJAXMessage(this, "Only file with extension .PDF/.DOC/.DOCX Allowed to be Uploaded");
}
}
else
{
DisplayAJAXMessage(this, "Maximum file size upto 1MB");
}
return file;
}
---------------------------------------------------------------------------------
static public void DisplayAJAXMessage(Control page, string msg)
{
string myScript = String.Format("alert('{0}');", msg);
ScriptManager.RegisterStartupScript(page, page.GetType(), "MyScript", myScript, true);
}
Note : The "FolderName" should be a folder where the uploaded file is to be kept in project.

No comments:
Post a Comment