protected void btn_upload_Click(object sender, EventArgs e)
{
// check txt file before uploading
if (txt_title.Text=="")
{
lbl_message_page.Text = "File Name Should Be Inserted";
txt_title.Focus();
return;
}
// generate random number (ID in database)
string num = "0123456789";
int lenght = num.Length;
string otp = string.Empty;
int otpdigit = 5;
string finaldigit;
int getindex;
for (int i = 0; i < otpdigit; i++)
{
do
{
getindex = new Random().Next(0, lenght);
finaldigit = num.ToCharArray()[getindex].ToString();
} while (otp.IndexOf(finaldigit) != -1);
otp += finaldigit;
}
txt_reg_file.Text = otp;
//End Of Generate Random Numbers-----------------------------------------------------------------
// Started Upload Files :
if (FileUpload1.HasFiles)
{
string ext = System.IO.Path.GetExtension(FileUpload1.FileName);
if (ext!=".pdf" && ext!=".docx" && ext !=".doc" && ext!=".text" && ext !=".jpg" && ext!=".txt")
{
lbl_message_page.ForeColor = Color.Red;
lbl_message_page.Text = "File you would uploaded not supported";
return;
}
string FileName = string.Empty;
FileName = FileName.ToString() + "_";
FileName = FileName + DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString();
FileName += DateTime.Now.Hour.ToString();
FileName += DateTime.Now.Minute.ToString();
FileName += DateTime.Now.Second.ToString();
FileUpload1.SaveAs(Server.MapPath("Files") + "//" + FileName + ext);
string path = "~//Files//"+FileName+ext;
string sql_upload_file = "INSERT INTO TBL_Files (File_ID,File_Title,File_Upload_Date,File_Path,File_Downloads) VALUES (@File_ID,@TITLE,@FILEUPDATE,@FPATH,@FILEDOWNS)";
SqlCommand cmd = new SqlCommand(sql_upload_file, sqlcon);
cmd.Parameters.AddWithValue("@File_ID", txt_reg_file.Text);
cmd.Parameters.AddWithValue("@TITLE", txt_title.Text);
cmd.Parameters.AddWithValue("@FILEUPDATE", DateTime.Now);
cmd.Parameters.AddWithValue("@FPATH", path);
cmd.Parameters.AddWithValue("@FILEDOWNS", 0);
sqlcon.Open();
cmd.ExecuteNonQuery();
sqlcon.Close();
lbl_message_page.ForeColor = Color.Green;
lbl_message_page.Text = String.Format("{0} file has been uploaded successfully.", FileUpload1.PostedFiles.Count);
lbl_file_type.Text +=FileUpload1.FileName;
// lbl_message_page.Text = "Data has been saved successfully";
}
else
{
lbl_message_page.ForeColor = Color.Red;
lbl_message_page.Text = "not Uploaded !";
}