簡體   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