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; }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)