文件大小在上载时使用typescript进行验证

时间:2017-09-08 04:59:39

标签: javascript jquery

我正在尝试在文件上传时使用typescript进行文件大小验证,但仍无法正常工作。 因为没有找到打字稿"尺寸"财产

请帮帮我

1 个答案:

答案 0 :(得分:0)

检查这个小提琴

您可以获取文件大小并从javascript中拒绝

https://jsfiddle.net/yogesh078/qger8xte/

使用Javascript:

document.getElementById('fileupload').addEventListener('change', checkFile, false);

    function checkFile(e) {

        var file_list = e.target.files;

        for (var i = 0, file; file = file_list[i]; i++) {
            var sFileName = file.name;
            var sFileExtension = sFileName.split('.')[sFileName.split('.').length - 1].toLowerCase();
            var iFileSize = file.size;
            var iConvert = (file.size / 10485760).toFixed(2);

            if (!(sFileExtension === "pdf" || sFileExtension === "doc" || sFileExtension === "docx") || iFileSize > 10485760) {
                txt = "File type : " + sFileExtension + "\n\n";
                txt += "Size: " + iConvert + " MB \n\n";
                txt += "Please make sure your file is in pdf or doc format and less than 10 MB.\n\n";
                alert(txt);
            }
        }
    }

HTML:

<input type="file" name="file" id="fileupload">