【问题标题】:allow file with a specific filename and extension to be uploaded允许上传具有特定文件名和扩展名的文件
【发布时间】:2016-07-26 16:19:18
【问题描述】:

我是 javascript 新手,我需要检查文件名是否只有在文件名具有特定名称和特定扩展名时才需要上传。我怎么做?例如:我只能上传文件名:can_to_do 和扩展名:pdf 否则它会发出警报,要求我们更改文件名

【问题讨论】:

标签: javascript html


【解决方案1】:

假设我需要上传 jrxml 文件扩展名。所以验证是这样的。说出文件名someFile.jrxml

var fileVal = $('#fileId').val();
    if (fileVal != null && fileVal != '')
         {
           var ext = fileVal.split('.').pop().toLowerCase();
           if ($.inArray(ext, ['jrxml']) == -1) {
              jAlert('Not a valid file');
              isOk = false;
           }
         }

【讨论】:

    【解决方案2】:

    我找到了我发布的这个问题的解决方案,即只有当文件具有特定的文件名和扩展名时才能上传。例如:“ReleaseNotes.txt”。它在 Javascript 中:

     function uploadFile() 
        {
        var fileElement = document.getElementById("fileToUpload");
                var x = document.getElementById("fileToUpload");
                var txt = "";
              if ('files' in x) {
              if (x.files.length != 0) {
              for (var i = 0; i < x.files.length; i++) {
              var file = x.files[i];
              if ('name' in file) {
              if (file.name == "ReleaseNotes.txt")
              {
              alert("pass");    
              }
              else{
              alert("fail");
        }   
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多