Bhanu Prakash Bhanu Prakash - 1 year ago 95
Javascript Question

Query Regarding File Upload

I am using "File Upload" control and C# coding (backend) for uploading files(.jpeg/.png/.pdf) in my web application.
The files uploaded through this control should be saved in the server.
Everything is working fine but the problem i am facing is when a file type of xlsx or doc is been saved and the extension of that file is changed to .png or .jpeg and is being uploaded it is being uploaded into the server without any error.

While I am trying to open that image or pdf file in server it is as usually showing the error message the file cant be opened.

I have done extension validation but it does not show any effect here in this case.

Can anyone help me to get rid of this problem.(Either C# coding or Jquery Or javascript will do)

Answer Source

Here is how finally able to get the validation as I need using "Header codes":

 System.IO.BinaryReader r = new System.IO.BinaryReader(FileUpload1.PostedFile.InputStream);
                                string fileclass = "";
                                byte buffer;
                                    buffer = r.ReadByte();
                                    fileclass = buffer.ToString();
                                    buffer = r.ReadByte();
                                    fileclass += buffer.ToString();
                                if (fileclass != "3780" || fileclass != "255216" || fileclass != "13780")    /*Header codes (3780-PDF);(255216-JPG,JPEG);(13780-PNG)*/
                                    /*Your code goes here(things to do with the file uploaded)*/

For getting values for other file formats try uploading the file and set break point and get the header code.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download