代码之家  ›  专栏  ›  技术社区  ›  Zidane

如何正确使用javascript“includes”方法检查字符串是否包含某些字符[重复]

  •  0
  • Zidane  · 技术社区  · 7 年前

    function submitFunction()
    {
        // $('#fileLabel').val("");
        $('uploadLoader').removeClass("done");
        document.getElementById("uploadLoader").style.display = "none";
        $.msgbox("Your file has been uploaded");
        //$("uploadLoader").hide();
    
        var input, file;
        var extension = file.name; 
    
        if (!window.FileReader) {
            $.msgbox("p", "The file API isn't supported on this browser yet.");
                return;
        }
    
        input = document.getElementById('fileinput');
        if (!input) {
            bodyAppend("p", "Um, couldn't find the fileinput element.");
        }
        else if (!input.files) {
            $.msgbox("p", "This browser doesn't seem to support the `files` " 
                          + "property of file inputs.");
        }
        else if (!input.files[0]) {
            $.msgbox("p", "Please select a file before clicking 'Load'");
        }
        else if (!extension.includes("xls")) {
            $.msgbox("Please select an xls or xlsx file");
        }
        else if (!extension.includes("xlsx")) {
            $.msgbox("Please select an xls or xlsx file");
        }
        else {
            file = input.files[0];
            $.msgbox("p", "File " + file.name + " is " + file.size 
                            + " bytes in size");
        }
    }
    
    
    
    
    <iframe width="0" height="0" border="0" name="dummyframe" style="display: none;" 
            id="dummyframe"></iframe>
    <form id="uploadForm" name="form1" method="post" enctype="multipart/form-data" 
        action="/api/BulkUpload" target="dummyframe" onsubmit="submitFunction()">
        <div>
            @*<label for="caption">Upload Bulk File</label>*@
            @*<input name="caption" type="text" />*@
        </div>
        <div id="inputLabel">
            @*<input id="fileLabel" name="image1" type="file" />*@
            <input id="fileinput" name="image1" type="file" />
        </div>
        <div>
            <span class="btn btn-success fileinput-button">
                <i class="icon-plus icon-white"></i>
                <span>upload file</span>
                <input id="submitButton"class="submit" type="submit" 
                        value="ok" onclick="validation()"/>
            </span>
        </div>
    </form>
    
    1 回复  |  直到 7 年前
        1
  •  0
  •   Ashish Yadav    7 年前

    您可以弹出文件的扩展名并检查值,而不是includes方法。

    var file = input.file[0]; // I don't see your file where is it?
    var extension = file.name;
    
    // Split your file name "yourfile.ext" with dot "."
    // pop method will return the last element of array.
    var poppedExt = extension.split('.').pop();
    

    你可以检查poppedExt的欲望扩展。

    if(poppedExt == "xls" || poppedExt == "xlsx") { // alert something }
    
    if(file.size <= 5000000) { // alert something } // file.size returns bytes so 5000000 is 5MB, I have used online tool to convert byte to mb.
    

    希望这有帮助。