返回首页

嗨,我有一个问题FileHeaderEncode核查后保存文件。

我只是想验证,上传的文件实际上是一个JPEG图像,不能只是说,一个文本文件更名为。JPEG延长。

为了验证这一点,我用的方法,我称之为"DetectFileheaderEncode"。但每当我使用此方法,并尝试保存的图像,它保存的图像大小为0,因此,形象是不可用?

可以请你帮我吗?

谢谢你,
codddy

代码如下:

 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;

        }

回答

评论会员:c 时间:2