繁体   English   中英

上传和验证-打字稿和角度

[英]Upload and validation - Typescript and angular

这只是应用程序的功能,可在上传前检查文件扩展名和大小。 无需读取数据。 示例将不胜感激。 谢谢

这也可以通过普通的javascript实现。 打字稿是javascript的超集。 您可以编写一个函数来检查文件大小和扩展名。 在此之前,您需要从视图向函数发送事件。

所以在角度上,你可以尝试像

<input type="file" (change)="fileEvent($event)" >

在打字稿中,您可以编写一个函数来检查事件,例如

 fileEvent(e) {

    /// get list of files
    let file_list = e.target.files;

    /// go through the list of files
    for (let i = 0, file; file = file_list[i]; i++) {

        let sFileName = file.name;
        let sFileExtension = sFileName.split('.')[sFileName.split('.').length - 1].toLowerCase();
        let iFileSize = file.size;
        let iConvert = (file.size / 1048576).toFixed(2);

        /// OR together the accepted extensions and NOT it. Then OR the size cond.
        /// It's easier to see this way, but just a suggestion - no requirement.
        if (!(sFileExtension === "pdf" ||
              sFileExtension === "doc" ||
              sFileExtension === "docx") || iFileSize > 10485760) { /// 10 mb
            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);
        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM