// Fires when Button is clicked.
protected void UploadFileOnServer(object sender, EventArgs e)
{
// limitation of maximum file size
int intFileSizeLimit = 10;
// get the full path of your computer
string strFileNameWithPath = FileUpload1.PostedFile.FileName;
// get the extension name of the file
string strExtensionName = System.IO.Path.GetExtension(strFileNameWithPath);
// get the filename of user file
string strFileName = System.IO.Path.GetFileName(strFileNameWithPath);
// get the file size
int intFileSize = FileUpload1.PostedFile.ContentLength / 1024; // convert into byte
// Restrict the user to upload only .gif or .jpg file
strExtensionName = strExtensionName.ToLower();
if (strExtensionName.Equals(".jpg") || strExtensionName.Equals(".gif"))
{
// Rstrict the File Size
if (intFileSize < intFileSizeLimit)
{
// upload the file on the server
// you can save the file with any name, However as this is the sample so I have saved the file same name for all users. So it will overwrite your file with next user's who will test this tutorials.
FileUpload1.PostedFile.SaveAs(Server.MapPath("~/UserFiles/Samples/") + "SampeFromTutorials" + strExtensionName);
lblMessage.Text = "Uploaded file details
" +
"File path on your Computer: " + strFileNameWithPath + "
" +
"File Name: " + strFileName + "
" +
"File Extension Name: " + strExtensionName + "
" +
"File Size: " + intFileSize.ToString();
}
else
{
lblMessage.Text = "File size exceeded than limit " + intFileSizeLimit + " KB, Please upload smaller file.";
}
}
else
{
lblMessage.Text = "Only .jpg or .gif file are allowed, try again!";
lblMessage.ForeColor = System.Drawing.Color.Red;
}
}







0 Post Comments / Add your comment:
Post a Comment