<pre lang="c#"><pre lang="c#">
protected void Upload_btn_Click(object sender, EventArgs e)
{
String fileName = Path.GetFileName(FileUpload1.PostedFile.FileName);
Panel_Sucess.Visible = false;
if (FileUpload1.HasFile)
{
if (File.Exists("D:\\WebSite examples\\myproject\\QuestionBank\\" + fileName))
{
Panel_error.Visible = true;
label_error.Text = "A file with the name
" + fileName + " already exists on the server.";
return;
}
int fileSize = FileUpload1.PostedFile.ContentLength;
if (fileSize > 2*1024 * 1024)
{
Panel_error.Visible = true;
label_error.Text = "Filesize of image is too large. Maximum file size permitted is 1 MB";
return;
}
string fileExtension = Path.GetExtension(fileName);
fileExtension = fileExtension.ToLower();
string[] acceptedFileTypes = new string[1];
acceptedFileTypes[0] = ".pdf";
bool acceptFile = false;
for (int i = 0; i <1; i++)
{
if (fileExtension == acceptedFileTypes[i])
{
acceptFile = true;
}
}
if (!acceptFile)
{
Panel_error.Visible = true;
label_error.Text = "The file you are trying to upload is not a permitted file type,file should be
.pdf ;
return;
}
Panel_error.Visible = false;
var path = "D:\\Website examples\\myproject\\QuestionBank\\";
var directoryinfo = new DirectoryInfo(path);
string s1 = (String)Session["t1"];
SqlConnection con = new SqlConnection("Data Source=USER-PC;Initial Catalog=myproject;User ID=sa;Password=Password1");
SqlCommand cmd = new SqlCommand("Select College from Registration_table where College='" + s1 + "'", con);
if (directoryinfo.Exists)
{
if (!Directory.Exists(path + s1))
{
var newfolder = directoryinfo.CreateSubdirectory("" + s1);
FileUpload1.SaveAs(Server.MapPath("~/" + newfolder + "\\") +fileName);
Response.Write("<script language='javascript'>window.alert('QuestionBank Successfully Uploaded');</script>");
}
else
Panel_error.Visible = true;
label_error.Text = "one College can upload only one Qbank";
}
}
}