【问题标题】:how to use the javascript "includes" method properly to check if a string contains certain characters [duplicate]如何正确使用javascript“包含”方法来检查字符串是否包含某些字符[重复]
【发布时间】:2018-11-06 14:47:50
【问题描述】:

我想知道如何正确使用 javascript "includes" 方法来检查字符串是否包含某些字符。这就是我下面的内容,但我认为它是错误的,因为 msgBox 没有弹出。我基本上想限制我的用户只上传到 xls 和 xlsx 类型的文件。如果文件大小 file.size 超过 5MB,我也想阻止上传。

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>

【问题讨论】:

  • file 未初始化,然后您执行 var extension = file.name 可能会因异常而失败。

标签: javascript html asp.net-mvc forms file-upload


【解决方案1】:

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

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.

希望这会有所帮助。

【讨论】:

    猜你喜欢
    • 2015-03-15
    • 1970-01-01
    • 2016-08-09
    • 2011-03-31
    • 2014-05-07
    • 2018-05-01
    • 2019-12-29
    • 2012-04-21
    相关资源
    最近更新 更多