C# Code on button click:
// Read the file and convert it to Byte Array
string filePath = FileUpload1.PostedFile.FileName;
string filename = Path.GetFileName(filePath);
string ext = Path.GetExtension(filename);
string contenttype = String.Empty;
//Set the contenttype based on File Extension
switch (ext)
{
case ".jpg":
contenttype = "image/jpg";
break;
case ".jpeg":
contenttype = "image/jpeg";
break;
case "pdf":
contenttype = "text/pdf";
break;
}
if (contenttype != String.Empty)
{
Stream fs = FileUpload1.PostedFile.InputStream;
BinaryReader br = new BinaryReader(fs);
Byte[] bytes = br.ReadBytes((Int32)fs.Length);
cmd.Parameters.AddWithValue("@img", bytes);
}
Sir,
as you can see an above code. This code would execute on button click. I have checked a type of file uploaded in FileUpload Control by verifying extension.
Sir, by cheking only extension of file dosen't prove that the file which going to upload is really a .jpg file or .pdf file because a user can change text file to pdf or txt fle to jpg or to any other type of file just by changing extension.
Therefore I want to check originality of uploaded file by using it's Magic Number OR using any other method (if available) so that user could never cheat or upload wrong file.