Click here to Skip to main content
15,942,148 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, I'm having a problem saving a file after FileHeaderEncode verification.

I am just trying to verify that the uploaded document is actually a jpeg image, not just say, a Text file renamed to have a .jpeg extension.

To verify this I use method which I called "DetectFileheaderEncode". But whenever I use this method and try to save the image, it saves the image as 0 size therefore, the image is not available?

Could you please help me?

Thank you,

codddy

The code is as below:
C#
 protected void btnUpload_Click(object sender, EventArgs e)
        {
            String savePath = @"C:\\Users\\xx\\Desktop\\fileupload\\";

            try
            {
                if (FileUpload1.HasFile)
                {
                    string fileName = Server.HtmlEncode(FileUpload1.FileName);

                    Boolean verified = VerifyUploadJpegImage(FileUpload1);

                    if (verified) 
                    {

                        // Append the name of the file to upload to the path.
                        savePath += fileName;

                        FileUpload1.SaveAs(savePath);

                        // Notify the user that their file was successfully uploaded.
                        lblUploadStatus.Text = "Your file was uploaded successfully.";
                        lblUploadStatus.ForeColor = Color.Green;
                    }
                    else
                    {
                        // Invalid upload file
                        // notify the user why their file was not uploaded.
                        lblUploadStatus.Text = "Your file was not uploaded because " +
                                                 "it is not a jpegImage";
                        lblUploadStatus.ForeColor = Color.Red;
                    }

                }
                else
                {
                    lblUploadStatus.Text = "You have not specified a file.";
                }
            }
            catch (Exception ex)
            {
                lblUploadStatus.Text = "ERROR:" + ex.Message;
            }

        }


private Boolean VerifyUploadJpegImage(FileUpload fileUpload)
        {
            Boolean verified = false;

            String fileName = Server.HtmlEncode(fileUpload.FileName);

            String extension = System.IO.Path.GetExtension(fileName);

            String contentType = fileUpload.PostedFile.ContentType;


            String fileheaderEncoding = DetectFileheaderEncode(fileUpload);

            if (((extension == ".jpg") || (extension == ".jpeg")) && (contentType == "image/jpeg") && (fileheaderEncoding == "255216"))
            {
                verified = true;
            }

            return verified;
        }

        private String DetectFileheaderEncode(FileUpload fileUpload)
        {
            String encode = String.Empty;
           
            System.IO.BinaryReader r = new System.IO.BinaryReader(fileUpload.PostedFile.InputStream);
            string fileclass = "";
            byte buffer;
            try
            {
                buffer = r.ReadByte();
                fileclass = buffer.ToString();
                buffer = r.ReadByte();
                fileclass += buffer.ToString();
            }
            catch(Exception ex)
            {

            }

            r.Close();

            encode = fileclass;

            return encode;
        }
Posted
Updated 11-Oct-11 22:03pm
v2
Comments
André Kraak 12-Oct-11 3:31am    
Obvious question, but does it work when you skip VerifyUploadJpegImage?
codddy 12-Oct-11 4:09am    
Yes, if I skip verification, in fact only if I skip "DetectFileheaderEncode(FileUpload fileUpload)" method it works.

It's something wrong with the reader - maybe r.Close()- I think, but couldn't get around it.

Thanks

1 solution

Ok I solved the problem. :)

I just commented out r.Close() in DetectFileheaderEncode method, and it's working correctly now.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900