Uploading files works fine but now I'm trying to validate file extensions and looks like there's some interference
between FileUpload1
and FileUpload2
.
FileUpload1
is used for uploading .jpg or .png images, and FileUpload2
for uploading .pdf files.
Here's the code which is executed on BtnInsert_Click
event:
protected void BtnInsert_Click(object sender, EventArgs e)
{
string[] validPhotoFile = { ".jpg", ".png" };
string validPDFFile = ".pdf";
string photoExt = System.IO.Path.GetExtension(FileUpload1.PostedFile.FileName);
string pdfExt = System.IO.Path.GetExtension(FileUpload2.PostedFile.FileName);
bool isValidPhotoFile = false;
bool isValidPDFFile = false;
for (int i = 0; i < validPhotoFile.Length; i++)
{
if (photoExt == "." + validPhotoFile[i])
{
isValidPhotoFile = true;
break;
}
}
for (int i = 0; i < validPDFFile.Length; i++)
{
if (pdfExt == "." + validPDFFile[i])
{
isValidPDFFile = true;
break;
}
}
if (!isValidPhotoFile)
{
PhotoErrorMessage.Text = "Upload .jpg or .png image!";
}
if (!isValidPDFFile)
{
PDFErrorMessage.Text = "Upload .pdf file!";
}
else
{
string photoFilPath = Path.GetFileName(FileUpload1.PostedFile.FileName.ToString());
string pdfFilPath = Path.GetFileName(FileUpload2.PostedFile.FileName.ToString());
string photoPath = Server.MapPath(@"~/PDFCover/" + fotoFilPath);
string pdfPath = Server.MapPath(@"~/PDF/" + pdfFilPath);
FileUpload1.PostedFile.SaveAs(photoPath);
FileUpload2.PostedFile.SaveAs(pdfPath);
SqlCommand cmd = new SqlCommand("INSERT INTO Book(Title,Content...) VALUES ('" + TextBox1.Text
+ "','" + TextBox2.Text + ... + "','" + "~/PDFCover/" + photoFilPath
+ "','" + "~/PDF/" + pdfFilPath + "')", con);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
Now even if I choose to upload valid files it's showing label error messages to upload valid files.