简体   繁体   English

允许上传具有特定文件名和扩展名的文件

[英]allow file with a specific filename and extension to be uploaded

I am new to javascript and I need to check that the filename has to be uploaded only if it has the specific name and a specific extension.我是 javascript 新手,我需要检查文件名是否只有在具有特定名称和特定扩展名时才必须上传。 How do i do that?我怎么做? For example: I can upload file only if it has filename: can_to_do and extension: pdf else it will send out an alert asking us to change the filename例如:我只能上传文件名:can_to_do 和扩展名:pdf 否则它会发出警报,要求我们更改文件名

Suppose i need to upload jrxml file extension.假设我需要上传 jrxml 文件扩展名。 so the validation goes something like that.所以验证是这样的。 say file name someFile.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;
           }
         }

I found the solution for this question I posted that is to be able to upload only if the file has a particular filename and extension.我找到了我发布的这个问题的解决方案,即只有在文件具有特定文件名和扩展名时才能上传。 Here, for example: "ReleaseNotes.txt".在这里,例如:“ReleaseNotes.txt”。 It is in Javascript:它在 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");
    }   
}

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

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