Friday, February 13, 2009

File Upload to server

using System.IO;
////////////

if (fupApplicationForms.HasFile)
{
string[] FileType = new string[] { ".doc", ".pdf", ".docx" };
string FileExtension = Path.GetExtension(fupApplicationForms.FileName).ToLower();
Boolean FileOk = false;
for (int i = 0; i < FileType.Length; i++)
{
if (FileExtension == FileType[i])
{
FileOk = true;
}
}
if (FileOk == true)
{

ApplicationFormsSP apsp = new ApplicationFormsSP();
string str, formname;
str = System.IO.Path.GetFileName(fupApplicationForms.PostedFile.FileName);
formname = Server.MapPath(".\\") + "ApplicationForms\\" + str;
FileInfo apform = new FileInfo(formname);
if (apform.Exists)
{
lblError.Text = "Sorry! A file with the name you specified already exists. Specify a different filename";

}
else
{

fupApplicationForms.PostedFile.SaveAs(formname);
int size = fupApplicationForms.PostedFile.ContentLength;
double kbsize = (double)size / 1024;
kbsize = Math.Round(kbsize, 2);
string sizeikb = kbsize + " KB";
apsp.AddApplicationForm(str, sizeikb);


}
}
else
{
lblError.Text = "File extention doesn't support";

}
}
else
{
lblError.Text = "Nothing to Upload";

}

No comments: