Wednesday 20 April 2022

Upload valid file in C#


    protected bool CheckFileExtandLength(HttpPostedFile HtmlDocFile)
    {
        try
        {
            Dictionary<string, byte[]> imageHeader = new Dictionary<string, byte[]>();
            imageHeader.Add("JPG", new byte[] { 0xFF, 0xD8, 0xFF, 0xE0 });
            imageHeader.Add("JPEG", new byte[] { 0xFF, 0xD8, 0xFF, 0xE0 });
            imageHeader.Add("PNG", new byte[] { 0x89, 0x50, 0x4E, 0x47 });
            imageHeader.Add("TIF", new byte[] { 0x49, 0x49, 0x2A, 0x00 });
            imageHeader.Add("TIFF", new byte[] { 0x49, 0x49, 0x2A, 0x00 });
            imageHeader.Add("GIF", new byte[] { 0x47, 0x49, 0x46, 0x38 });
            imageHeader.Add("BMP", new byte[] { 0x42, 0x4D });
            imageHeader.Add("ICO", new byte[] { 0x00, 0x00, 0x01, 0x00 });
            imageHeader.Add("PDF", new byte[] { 0x25, 0x50, 0x44, 0x46 });
            //
            imageHeader.Add("XLS", new byte[] { 0xD0, 0xCF, 0X11, 0xE0 });
            imageHeader.Add("XLSX", new byte[] { 0x50, 0x4B, 0x03, 0x04 });
            imageHeader.Add("DOC", new byte[] { 0xD0, 0xCF, 0X11, 0xE0 });
            imageHeader.Add("DOCX", new byte[] { 0x50, 0x4B, 0x03, 0x04 });
            imageHeader.Add("TXT", new byte[] { 0x3C, 0x68, 0x74, 0x6D });
            Int32 f_size = 2;
            bool IsValid = true;
            var supportedTypes = new[] { "pdf", "tif", "xls", "xlsx", "doc", "docx", "txt", "jpg", "gif", "bmp", "png" };
            var contentTypes = new[] { "application/pdf", "image/tiff", "application/vnd.ms-excel", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "application/msword", "application/vnd.openxmlformats-officedocument.wordprocessingml.document", "text/plain", "image/jpeg", "image/gif", "image/bmp", "image/png" };
            var fileExt = System.IO.Path.GetExtension(HtmlDocFile.FileName).Substring(1).ToLower();
            if (!supportedTypes.Contains(fileExt))
            {
                IsValid = false;
            }
            if (HtmlDocFile.ContentLength <= 0)
            {
                IsValid = false;
            }
            if (HtmlDocFile.ContentLength > (f_size * 1024 * 1024))
            {
                IsValid = false;
            }
            if (!contentTypes.Contains(HtmlDocFile.ContentType))
            {
                IsValid = false;
            }
            byte[] header;
            byte[] tmp = imageHeader[fileExt.ToUpper()];
            header = new byte[tmp.Length];
            // GET HEADER INFORMATION OF UPLOADED FILE
            HtmlDocFile.InputStream.Read(header, 0, header.Length);
            if (!CompareArray(tmp, header))
            {
                return false;
            }
            return IsValid;
        }
        catch (Exception)
        {
            return false;
        }
    }
    private bool CompareArray(byte[] a1, byte[] a2)
    {
        if (a1.Length != a2.Length)
            return false;
        for (int i = 0; i < a1.Length; i++)
        {
            if (a1[i] != a2[i])
                return false;
        }
        return true;
    }

Upload valid file in C#

    protected bool CheckFileExtandLength(HttpPostedFile HtmlDocFile)     {         try         {             Dictionary<string, byte[]>...