判断文件真实类型

JS端判断:

真实文件代码列表:

https://www.garykessler.net/library/file_sigs.html

   //https://www.garykessler.net/library/file_sigs.html
    //https://stackoverflow.com/questions/18299806/how-to-check-file-mime-type-with-javascript-before-upload
    /**
 * Load the mime type based on the signature of the first bytes of the file
 * @param  {File}   file        A instance of File
 * @param  {Function} callback  Callback with the result
 * @author Victor www.vitim.us
 * @date   2017-03-23
 */
    function loadMime(file, callback) {

        //List of known mimes
        var mimes = [
            {
                mime: 'image/jpeg',
                pattern: [0xFF, 0xD8, 0xFF],
                mask: [0xFF, 0xFF, 0xFF],
            },
            {
                mime: 'image/png',
                pattern: [0x89, 0x50, 0x4E, 0x47],
                mask: [0xFF, 0xFF, 0xFF, 0xFF],
            },
            {
                mime: 'pdf',
                pattern: [0x25, 0x50, 0x44, 0x46],
                mask: [0xFF, 0xFF, 0xFF, 0xFF],
            }
            // you can expand this list @see https://mimesniff.spec.whatwg.org/#matching-an-image-type-pattern
        ];

        function check(bytes, mime) {
            for (var i = 0, l = mime.mask.length; i < l; ++i) {
                if ((bytes[i] & mime.mask[i]) - mime.pattern[i] !== 0) {
                    return false;
                }
            }
            return true;
        }

        var blob = file.slice(0, 4); //read the first 4 bytes of the file

        var reader = new FileReader();
        reader.onloadend = function (e) {
            if (e.target.readyState === FileReader.DONE) {
                var bytes = new Uint8Array(e.target.result);

                for (var i = 0, l = mimes.length; i < l; ++i) {
                    //if (check(bytes, mimes[i])) return callback("Mime: " + mimes[i].mime + " <br> Browser:" + file.type);
                    if (check(bytes, mimes[i])) return callback(mimes[i].mime);
                }
                return callback("unknown");
                //return callback("Mime: unknown <br> Browser:" + file.type);
            }
        };
        reader.readAsArrayBuffer(blob);
    }

    function readFile(file, callback) {
        var reader = new FileReader();
        reader.onload = callback
        reader.readAsDataURL(file);//base64 code file
        //debugger;
    }

上传按钮事件判断:

 loadMime(e.target.files[0], function (mime) {
            if (mime == 'unknown') {
                //do something
                
                return;
            }
            switch (mime) {
                case "pdf":
                   readFile(e.target.files[0], function (e) {
                        //do something
                    });
                    break;
                case "image/jpeg":
                case "image/png":
                   readFile(e.target.files[0], function (e) {
                        //do something
                    });
                    break;
            }            
        });

服务器端判断

真实文件类型列表:

  1. JPG = 255216,

  2. GIF = 7173,

  3. BMP = 6677,

  4. PNG = 13780,

  5. COM = 7790,

  6. EXE = 7790,

  7. DLL = 7790,

  8. RAR = 8297,

  9. ZIP = 8075,

  10. XML = 6063,

  11. HTML = 6033,

  12. ASPX = 239187,

  13. CS = 117115,

  14. JS = 119105,

  15. TXT = 210187,

  16. SQL = 255254,

  17. BAT = 64101,

  18. BTSEED = 10056,

  19. RDP = 255254,

  20. PSD = 5666,

  21. PDF = 3780,

  22. CHM = 7384,

  23. LOG = 70105,

  24. REG = 8269,

  25. HLP = 6395,

  26. DOC = 208207,

  27. XLS = 208207,

  28. DOCX = 208207,

  29. XLSX = 208207,

  30.  
Private Function myFileTrueType(ByVal file As HttpPostedFileBase) As String
        Dim _Buf As Byte() = New Byte(file.InputStream.Length) {}
        file.InputStream.Read(_Buf, 0, file.InputStream.Length)
        Dim _Stream As IO.MemoryStream = New IO.MemoryStream(_Buf)
        Dim _BinReader As IO.BinaryReader = New IO.BinaryReader(_Stream)
        Dim _FileClass As String = String.Empty
        Dim _Buffer As Byte = _BinReader.ReadByte
        _FileClass = _Buffer.ToString
        _Buffer = _BinReader.ReadByte
        _FileClass += _Buffer.ToString
        _BinReader.Close()
        _Stream.Dispose()
        ''获取文件真实类型结束
        ''文件类型说明
        ''JPG = 255216
        ''GIF = 7173
        ''BMP = 6677
        ''PDF = 3780
        ''doc xls 208207
        ''pptx docx 8075         
        ''mp4 00
        Return _FileClass
    End Function

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值